Reputation: 2843
I'm using an Anaconda install of Python 3.6.3 and IPython 6.1.0 on Red Hat Enterprise Linux 7.5 . I have text files containing Unicode characters that I'm trying to work with, but I get the following error when trying to print the contents of these files:
UnicodeEncodeError: 'ascii' codec can't encode character '\U0001f644' in position 1: ordinal not in range(128)
I believe this is due to IPython defaulting to an ASCII encoding:
In [1]: from IPython.utils.encoding import get_stream_enc; import sys
In [2]: get_stream_enc(sys.stdout)
Out[2]: 'ANSI_X3.4-1968'
I can't find anything in the IPython documentation explaining how to change this to UTF-8. Is it possible?
Upvotes: 1
Views: 415
Reputation: 55589
iPython takes its output encoding from the environment. To use a unicode-aware encoding, either change your user's locale settings or set the PYTHONIOENCODING environment variable when calling iPython:
PYTHONIOENCODING=UTF-8 ipython
Upvotes: 1