Datasets icon indicating copy to clipboard operation
Datasets copied to clipboard

数据集文件 "FSPC/FSPC_V1.0.json" 没有用正确的编码存储,导致了乱码

Open ivy503 opened this issue 2 years ago • 0 comments

Python代码中 json.dumps() 函数在存储中文的时候需要加入参数 ensure_ascii=False 才能正确存储数据到json文件。不然就会产生现在这样的编码错误。 示例代码如下:

>>> json_string = json.dumps("ברי צקלה", ensure_ascii=False).encode('utf8')
>>> json_string
b'"\xd7\x91\xd7\xa8\xd7\x99 \xd7\xa6\xd7\xa7\xd7\x9c\xd7\x94"'
>>> print(json_string.decode())
"ברי צקלה"

正确的用法:

with io.open('filename', 'w', encoding='utf8') as json_file:
    data = json.dumps(u"ברי צקלה", ensure_ascii=False)
    # unicode(data) auto-decodes data to unicode if str
    json_file.write(unicode(data))

感谢项目组大大能修正下数据文件。谢谢!

ivy503 avatar Jan 13 '22 14:01 ivy503