Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ivy503 opened this issue Jan 13, 2022 · 0 comments

Comments

@ivy503
Copy link

ivy503 commented Jan 13, 2022

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))

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant