我的代码中有以下几行
outs = codecs.getwriter('utf-8')(sys.stdout)
# dJSON contains JSON message with non-ASCII chars
outs.write(json.dumps(dJSON,encoding='utf-8', ensure_ascii=False, indent=indent_val))
我收到以下异常:
outs.write(json.dumps(dJSON,encoding='utf-8', ensure_ascii=False, indent=indent_val))
File "/usr/lib/python2.7/json/__init__.py", line 238, in dumps
**kw).encode(obj)
File "/usr/lib/python2.7/json/encoder.py", line 204, in encode
return ''.join(chunks)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 27: ordinal not in range(128)
通过encoding='utf-8'
在json.dumps
声明中指定,我避免了此类问题。为什么我仍然收到错误?