Skip to content

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

Open

Description

@ivy503

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

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions