>>> with open("emoji.csv", encoding='utf-8') as f:
... lines = f.readlines()
>>> lines
['this line has an emoji \U0001f644\n']
没有问题。然而,当我在ipython6.1.0中尝试同样的方法时,我得到了以下结果
UnicodeEncodeError
In [1]: with open('emoji.csv', encoding='utf-8') as f:
...: lines = f.readlines()
...:
In [2]: lines
Out[2]: ---------------------------------------------------------------------------
UnicodeEncodeError Traceback (most recent call last)
<ipython-input-2-3fb162a4fe05> in <module>()
----> 1 lines
/opt/anaconda/lib/python3.6/site-packages/IPython/core/displayhook.py in __call__(self, result)
259 self.fill_exec_result(result)
260 if format_dict:
--> 261 self.write_format_data(format_dict, md_dict)
262 self.log_output(format_dict)
263 self.finish_displayhook()
/opt/anaconda/lib/python3.6/site-packages/IPython/core/displayhook.py in write_format_data(self, format_dict, md_dict)
188 result_repr = '\n' + result_repr
189
--> 190 print(result_repr)
191
192 def update_user_ns(self, result):
UnicodeEncodeError: 'ascii' codec can't encode character '\U0001f644' in position 24: ordinal not in range(128)
类似地,如果我尝试简单地对Unicode字符本身进行编码和解码,我会得到相同的错误:
In [1]: '\U0001f644'.encode('utf-8').decode('utf-8')
Out[1]: ---------------------------------------------------------------------------
...
...
UnicodeEncodeError: 'ascii' codec can't encode character '\U0001f644' in position 1: ordinal not in range(128)
:这似乎是IPython的一个函数,默认情况下使用ASCII编码:
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'