我相信unicode三明治。我使用unicode三明治。那为什么当我在一个字节字符串(py 2.7)上运行以下命令时呢?
label = label.decode("utf-8")
我仍然收到错误:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/celery/app/trace.py", line 385, in trace_task
R = retval = fun(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/celery/app/trace.py", line 648, in __protected_call__
return self.run(*args, **kwargs)
File "/opt/celery/cl/scrapers/tasks.py", line 638, in update_docket_info_iquery
d = update_docket_metadata(d, report.metadata)
File "/usr/local/lib/python2.7/site-packages/juriscraper/pacer/case_query.py", line 166, in metadata
self._get_label_value_pair(bold, True, field_names)
File "/usr/local/lib/python2.7/site-packages/juriscraper/pacer/docket_report.py", line 233, in _get_label_value_pair
label = label.decode("utf-8") <---- Shouldn't this work?
File "/usr/local/lib/python2.7/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 6: ordinal not in range(128)
而且,当我尝试在崩溃的行上执行decode时为什么会抛出UnicodeEncodeError
?
我很困惑。再次。
您的日志显示答案:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 6: ordinal not in range(128)
Python 2.7无法解码字符串中的字符,因为它是非ASCII字符。解决方案是完全使用unicode,或者先对其进行编码,然后使用适当的编解码器对其进行解码。