所以我收到了错误消息:
Traceback (most recent call last):
File "make.py", line 48, in <module>
json.dump(amazon_review, outfile)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 189, in dump
for chunk in iterable:
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 434, in _iterencode
for chunk in _iterencode_dict(o, _current_indent_level):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 408, in _iterencode_dict
for chunk in chunks:
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 313, in _iterencode_list
yield buf + _encoder(value)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xea in position 173: invalid continuation byte在这些代码中:
with open('amazon_review.json', 'w') as outfile:
json.dump(amazon_review, outfile)我会想出办法的。任何帮助都是最好的。
发布于 2014-06-03 13:22:45
Python2不使用Unicode接口,即使它返回Unicode字符串,因此它永远不会正确读取非ANSI字符。
因此,在使用以下代码将其编码回ASCII.Try之前,尝试使用.encode获取Unicode字符串会失败,并显示Unicode解码错误。
with open('amazon_review.json', 'w') as outfile:
try:
json.dump(amazon_review, outfile)# omit in 3.x!
except UnicodeEncodeError:
pass发布于 2014-06-03 13:25:21
我们可能需要更多地了解您传递给json.dump的数据,但我知道该api支持默认为utf-8的encoding kwarg。
你试过像这样的东西吗?
with open('amazon_review.json', 'w') as outfile:
json.dump(amazon_review, outfile, encoding="utf-16")看看这个similar issue可能是值得的
发布于 2014-06-04 02:23:33
在amazon_review的结构中有一个字节字符串。您应该确保只将Unicode字符串写入您打算用json.dump序列化的结构,因为JSON只能表示基于Unicode的字符串(没有与Python2的str相匹配的字节字符串的概念)。
只要字节字符串只包含ASCII字符,Python就可以处理这个错误,因为可以很好地猜测该字节字符串表示的任何编码都是ASCII超集。但是对于像0xEA这样的顶位字节,它不能猜测,所以在将结果传递到amazon_review之前,您必须通过在字节字符串上调用.decode('whatever-encoding-it-is-in')来告诉它。
如果在您的数据中,0xEA应该表示带有U+00EA的e- ê,那么要尝试的编码应该是'iso-8859-1'或'windows-1252'。
https://stackoverflow.com/questions/24007524
复制相似问题