我使用chr方法创建了一个数组,并且json.dumps返回一个异常。
我已经尝试过改用json.dump并检查对象的类型。
hashToChar={}
for a in range(0,256):
hashToChar.update({hash(chr(a)):chr(a)})
fil=open("hashes.txt",'w')
fil.write(json.dumps(hashToChar))
我希望它通过json.dumps将字典写入文件,但是会抛出:
Traceback (most recent call last):
File "main.py", line 9, in <module>
fil.write(json.dumps(hashToChar))
File "/usr/local/lib/python2.7/json/__init__.py", line 244, in dumps
return _default_encoder.encode(obj)
File "/usr/local/lib/python2.7/json/encoder.py", line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/local/lib/python2.7/json/encoder.py", line 270, in iterencode
return _iterencode(o, 0)
UnicodeDecodeError: 'utf8' codec can't decode byte 0x83 in position 0: invalid start byte