2013-03-14 28 views
16

Tôi đã tạo một gói bằng cách sử dụng mã hóa utf-8.Máy tính xách tay IPython: Mã hóa mặc định là gì?

Khi gọi hàm, hàm trả về DataFrame, với cột được mã hóa bằng utf-8.

Khi sử dụng IPython tại dòng lệnh, tôi không gặp bất kỳ sự cố nào khi hiển thị nội dung của bảng này. Khi sử dụng Notebook, nó bị lỗi với lỗi 'utf8' codec can't decode byte 0xe7. Tôi đã đính kèm một bản phản hồi đầy đủ bên dưới.

Mã hóa thích hợp để làm việc với Notebook là gì?

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 
+0

Tôi đã xảy ra điều này khi tôi đã nhúng dấu ngoặc kép thông minh làm giá trị trong tên chỉ mục hoặc cột. Bạn không chắc chắn nên sử dụng mã hóa nào để sử dụng nó, nhưng khi tôi loại bỏ các dấu ngoặc kép thông minh, vấn đề đã biến mất. – bdiamante

+0

Tôi đã đặt cột thành latin-1 và lỗi đã biến mất nhưng chuỗi hiển thị các ký tự không xác định –

+0

Bạn có thể đăng mẫu mã tối thiểu thể hiện sự cố không? –

Trả lời

11

tôi đã cùng một vấn đề thời gian gần đây, và thực sự thiết lập mã hóa mặc định sang UTF-8 đã làm các trick:

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

Chạy sys.getdefaultencoding() mang lại 'ascii' về môi trường của tôi (Python 2.7.3), do đó Tôi đoán đó là mặc định.

Đồng thời xem this related questionIan Bicking's blog post on the subject.

+1

Không nên sử dụng setdefaultencoding, xem ví dụ [here] (http://stackoverflow.com/a/3580165/1661267), ví dụ, nó vô hiệu hóa lệnh 'print'. – mountrix

Các vấn đề liên quan