You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@nifi.apache.org by Nathan Maynes <na...@gmail.com> on 2019/07/11 15:49:41 UTC
Character Encoding Issue Implementing PyStreamCallback
I am troubleshooting an Execute script processor that takes a JSON response
from a web API and converts it to an HTML table. When I run the code on my
local Windows 10 machine, the processor errors out with an encoding error
very similar to the one mentioned in this Hortonworks forum post
<https://community.hortonworks.com/questions/48624/nifi-executescript-processor-unicodeencodeerror-as.html>.
The error is "Error: UnicodeEncodeError: 'ascii' codec can't encode
character ...". I am trying to maintain UTF-8 via:
IOUtils.toString(inputStream, StandardCharsets.UTF_8)
I am wondering if there is a flag I am missing from the Python json
library. The spec says "this module’s serializer sets ensure_ascii=True by
default" - https://docs.python.org/2/library/json.html#character-encodings
Any ideas?
Here is the full code in case something else stands out.
# class PyStreamCallback(StreamCallback):
# def __init__(self):
# pass
#
# def process(self, inputStream, outputStream):
# text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
# json_obj = json.loads(text)
# html = '<html><style> table, th, td {border: 1px solid
black;border-collapse: collapse;text-align: left;padding:
5px;vertical-align: text-top;}</style>'
# if 'person' in json_obj:
# text = "hello"
# for image in json_obj['person']['images']:
# image['url'] = "<a href=\"" + image['url'] + "\">" +
"<img src= \"" + (image['url']) + "\"></img></a>"
# if 'possible_persons' in json_obj:
# x = 1
# for person in json_obj['possible_persons']:
# if 'images' in person:
# for image in person['images']:
# image['url'] = "<a href=\"" + image['url'] +
"\">" + "<img src= \"" + (
# image['url']) + "\"></img></a>"
# html = html + convert(json_obj)
# html = html + '</html>'
#
# outputStream.write(bytearray(html.encode('utf-8')))
--
Nathan Maynes <http://bit.ly/115hXAt>
@nathanmaynes
--
Nathan Maynes <http://bit.ly/115hXAt>
@nathanmaynes