[英]UnicodeEncodeError: 'ascii' codec can't encode
Ia have the following data container which is constantly being updated: Ia具有以下不断更新的数据容器:
data = []
for val, track_id in zip(values,list(track_ids)):
#below
if val < threshold:
#structure data as dictionary
pre_data = {"artist": sp.track(track_id)['artists'][0]['name'], "track":sp.track(track_id)['name'], "feature": filter_name, "value": val}
data.append(pre_data)
#write to file
with open('db/json/' + user + '_' + product + '_' + filter_name + '.json', 'w') as f:
json.dump(data,f, ensure_ascii=False, indent=4, sort_keys=True)
but I am getting a lot of errors like this: 但我收到很多这样的错误:
json.dump(data,f, ensure_ascii=False, indent=4, sort_keys=True) File"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 190, in dump fp.write(chunk)
UnicodeEncodeError: 'ascii' codec can't encode character u'\’' in position 6: ordinal not in range(128)
json.dump(data,f, ensure_ascii=False, indent=4, sort_keys=True) File"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 190, in dump fp.write(chunk)
UnicodeEncodeError: 'ascii' codec can't encode character u'\’' in position 6: ordinal not in range(128)
Is there a way I can get rid of this encoding problem once and for all? 有没有一种方法可以一劳永逸地解决此编码问题?
I was told that this would do it: 有人告诉我可以这样做:
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
but many people do not recommend it. 但是很多人不建议这样做。
I use python 2.7.10
我使用python 2.7.10
any clues? 有什么线索吗?
When you write to a file that was opened in text mode, Python encodes the string for you. 当您写入以文本模式打开的文件时,Python会为您编码字符串。 The default encoding is ascii
, which generates the error you see; 默认编码为ascii
,它会生成您看到的错误。 there are a lot of characters that can't be encoded to ASCII. 许多字符无法编码为ASCII。
The solution is to open the file in a different encoding. 解决方案是使用不同的编码打开文件。 In Python 2 you must use the codecs
module, in Python 3 you can add the encoding=
parameter directly to open
. 在Python 2中,您必须使用codecs
模块,在Python 3中,您可以直接添加encoding=
参数来open
。 utf-8
is a popular choice since it can handle all of the Unicode characters, and for JSON specifically it's the standard; utf-8
是一种流行的选择,因为它可以处理所有Unicode字符,特别是对于JSON来说,它是标准。 see https://en.wikipedia.org/wiki/JSON#Data_portability_issues . 请参阅https://en.wikipedia.org/wiki/JSON#Data_portability_issues 。
import codecs
with codecs.open('db/json/' + user + '_' + product + '_' + filter_name + '.json', 'w', encoding='utf-8') as f:
Your object has unicode strings and python 2.x's support for unicode can be a bit spotty. 您的对象具有unicode字符串,而python 2.x对unicode的支持可能有些参差不齐。 First, lets make a short example that demonstrates the problem: 首先,让我们做一个简短的例子来说明问题:
>>> obj = {"artist":u"Björk"}
>>> import json
>>> with open('deleteme', 'w') as f:
... json.dump(obj, f, ensure_ascii=False)
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/usr/lib/python2.7/json/__init__.py", line 190, in dump
fp.write(chunk)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 3: ordinal not in range(128)
From the json.dump
help text: 从json.dump
帮助文本中:
If ``ensure_ascii`` is true (the default), all non-ASCII characters in the
output are escaped with ``\uXXXX`` sequences, and the result is a ``str``
instance consisting of ASCII characters only. If ``ensure_ascii`` is
``False``, some chunks written to ``fp`` may be ``unicode`` instances.
This usually happens because the input contains unicode strings or the
``encoding`` parameter is used. Unless ``fp.write()`` explicitly
understands ``unicode`` (as in ``codecs.getwriter``) this is likely to
cause an error.
Ah! 啊! There is the solution. 有解决方案。 Either use the default ensure_ascii=True
and get ascii escaped unicode characters or use the codecs
module to open the file with the encoding you want. 使用默认的ensure_ascii=True
并获取ascii转义的unicode字符,或使用codecs
模块以所需的编码打开文件。 This works: 这有效:
>>> import codecs
>>> with codecs.open('deleteme', 'w', encoding='utf-8') as f:
... json.dump(obj, f, ensure_ascii=False)
...
>>>
Why not encode the specific string instead? 为什么不编码特定的字符串呢? try, the .encode('utf-8')
method on the string that is raising the exception. 尝试在引发异常的字符串上使用.encode('utf-8')
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.