Trying to print a unicode string to standard output (SSH terminal) from management command in Django (Python 3.4) app:
UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 10: ordinal not in range(128)
The same thing happens when the output is redirected to a file (in my case, when I run the management command from a script triggered by a scheduled task and the output goes to a log file).
I opted for a cheap workaround (replacing Unicode characters by a question mark):
print(unicode_string.encode('ascii', 'replace'))
There is the following environment variable:
LANG=en_US.UTF-8
Also, sys.getdefaultencoding()
gives utf-8
.
And the terminal correctly prints Unicode characters from another source (curl http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-demo.txt
).