I'm administering some Python code in which I now see an error in the logs:
Traceback (most recent call last):
File "./app/core.py", line 772, in scrapeEmail
l.info('EMAIL SUBJECT: ', header['value'])
File "./app/__init__.py", line 44, in info
logging.info(str(datetime.utcnow()) + ' INFO ' + caller.filename + ':' + str(caller.lineno) + ' - ' + ' '.join([str(x) for x in args]))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xea' in position 25: ordinal not in range(128)
which I guess means that header['value']
contains differently encoded characters.
I searched around, and this SO answer suggests to "put .encode('utf-8')
at the end of the object for recent versions of Python".
This raised two questions for me:
- On what object do I need to use
.encode('utf-8')
. Onx
or onstr(x)
. So should it bestr(x.encode('utf-8'))
or onstr(x).encode('utf-8')
? - What does the writer mean with "recent versions of Python"? Can I still use
.encode('utf-8')
in Python2.7
?
Normally I would simply try it, but it is not easy (actually impossible) to find the string on which the error occurred. So I can't really test it.
A little help would be greatly appreciated here.