IPython Notebook:默认编码是什么?

24
我已经使用utf-8编码创建了一个包。调用函数时,它返回一个DataFrame,其中一列使用utf-8编码。在命令行使用IPython时,没有问题显示此表的内容。但是,在使用Notebook时,它会崩溃,并显示错误“'utf8' codec can't decode byte 0xe7”。下面是完整的回溯信息。请问应该使用什么编码才能在Notebook中正常工作?
UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-13-92c0011919e7> in <module>()
      3 ver = verif.VerificacaoNA()
      4 comp, total = ver.executarCompRealFisica(DT_INI, DT_FIN)
----> 5 comp

c:\Python27-32\lib\site-packages\ipython-0.13.1-py2.7.egg\IPython\core\displayhook.pyc in __call__(self, result)
    240             self.update_user_ns(result)
    241             self.log_output(format_dict)
--> 242             self.finish_displayhook()
    243 
    244     def flush(self):

c:\Python27-32\lib\site-packages\ipython-0.13.1-py2.7.egg\IPython\zmq\displayhook.pyc in finish_displayhook(self)
     59         sys.stdout.flush()
     60         sys.stderr.flush()
---> 61         self.session.send(self.pub_socket, self.msg, ident=self.topic)
     62         self.msg = None
     63 

c:\Python27-32\lib\site-packages\ipython-0.13.1-py2.7.egg\IPython\zmq\session.pyc in send(self, stream, msg_or_type, content, parent, ident, buffers, subheader, track, header)
    557 
    558         buffers = [] if buffers is None else buffers
--> 559         to_send = self.serialize(msg, ident)
    560         flag = 0
    561         if buffers:

c:\Python27-32\lib\site-packages\ipython-0.13.1-py2.7.egg\IPython\zmq\session.pyc in serialize(self, msg, ident)
    461             content = self.none
    462         elif isinstance(content, dict):
--> 463             content = self.pack(content)
    464         elif isinstance(content, bytes):
    465             # content is already packed, as in a relayed message

c:\Python27-32\lib\site-packages\ipython-0.13.1-py2.7.egg\IPython\zmq\session.pyc in <lambda>(obj)
     76 
     77 # ISO8601-ify datetime objects
---> 78 json_packer = lambda obj: jsonapi.dumps(obj, default=date_default)
     79 json_unpacker = lambda s: extract_dates(jsonapi.loads(s))
     80 

c:\Python27-32\lib\site-packages\pyzmq-13.0.0-py2.7-win32.egg\zmq\utils\jsonapi.pyc in dumps(o, **kwargs)
     70         kwargs['separators'] = (',', ':')
     71 
---> 72     return _squash_unicode(jsonmod.dumps(o, **kwargs))
     73 
     74 def loads(s, **kwargs):

c:\Python27-32\lib\json\__init__.pyc in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, encoding, default, **kw)
    236         check_circular=check_circular, allow_nan=allow_nan, indent=indent,
    237         separators=separators, encoding=encoding, default=default,
--> 238         **kw).encode(obj)
    239 
    240 

c:\Python27-32\lib\json\encoder.pyc in encode(self, o)
    199         # exceptions aren't as detailed.  The list call should be roughly
    200         # equivalent to the PySequence_Fast that ''.join() would do.
--> 201         chunks = self.iterencode(o, _one_shot=True)
    202         if not isinstance(chunks, (list, tuple)):
    203             chunks = list(chunks)

c:\Python27-32\lib\json\encoder.pyc in iterencode(self, o, _one_shot)
    262                 self.key_separator, self.item_separator, self.sort_keys,
    263                 self.skipkeys, _one_shot)
--> 264         return _iterencode(o, 0)
    265 
    266 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,

UnicodeDecodeError: 'utf8' codec can't decode byte 0xe7 in position 199: invalid continuation byte

我曾经遇到过这种情况,当我在索引或列名中嵌入智能引号作为值时。不确定要使用哪种编码来解决它,但是当我删除智能引号时,问题就消失了。 - bdiamante
我已将列设置为latin-1,错误消失了,但字符串显示未知字符。 - Adriano Almeida
你能否发布一个最小的代码示例来演示问题? - Thomas K
1个回答

21

最近我也遇到了同样的问题,将默认编码设置为 UTF-8 确实起作用:

import sys
reload(sys)
sys.setdefaultencoding("utf-8")

在我的环境中(Python 2.7.3),运行sys.getdefaultencoding()返回'ascii',因此我猜这是默认设置。

还可以参考这个相关问题Ian Bicking关于该主题的博客文章


4
使用 setdefaultencoding 不是一个好主意,例如在这里(https://dev59.com/vnA65IYBdhLWcg3w4C6j#3580165)可以看到,它会禁用 print 命令。请注意,不建议使用该方法。 - mountrix
1
在Python 2上,默认为ascii编码。只有在Python 3上,默认为utf-8编码。 - Josir
这已经被弃用了:Python 3 没有 sys.setdefaultencoding() 函数。 - ttoshiro

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接