将中文数据转储到 json 文件中
Dump Chinese data into a json file
我在将中文数据(非拉丁语言数据)转储到 json 文件时遇到了问题。
我正在尝试使用以下代码将列表存储到 json
文件中;
with open("file_name.json","w",encoding="utf8") as file:
json.dump(edits,file)
它将转储而不会出现任何错误。
当我查看文件时,它看起来像这样,
[{sentence: \u5979\u7d30\u5c0f\u8072\u5c0d\u6211\u8aaa\uff1a\u300c\u6211\u501f\u4f60\u4e00\u679d\u925b\u7b46\u3002\u300d}...]
我也试过了,没有编码选项。
with open("file_name.json","w") as file:
json.dump(edits,file)
我的问题是,为什么我的 json 文件看起来像这样,以及如何使用 chinese 字符串而不是 unicode 字符串。
任何帮助将不胜感激。谢谢:)
查看 json.dump 的文档。
具体来说,它有一个开关 ensure_ascii
如果设置为 False
应该使函数不转义字符。
If ensure_ascii is true (the default), the output is guaranteed to have all incoming non-ASCII characters escaped. If ensure_ascii is false, these characters will be output as-is.
我在将中文数据(非拉丁语言数据)转储到 json 文件时遇到了问题。
我正在尝试使用以下代码将列表存储到 json
文件中;
with open("file_name.json","w",encoding="utf8") as file:
json.dump(edits,file)
它将转储而不会出现任何错误。 当我查看文件时,它看起来像这样,
[{sentence: \u5979\u7d30\u5c0f\u8072\u5c0d\u6211\u8aaa\uff1a\u300c\u6211\u501f\u4f60\u4e00\u679d\u925b\u7b46\u3002\u300d}...]
我也试过了,没有编码选项。
with open("file_name.json","w") as file:
json.dump(edits,file)
我的问题是,为什么我的 json 文件看起来像这样,以及如何使用 chinese 字符串而不是 unicode 字符串。 任何帮助将不胜感激。谢谢:)
查看 json.dump 的文档。
具体来说,它有一个开关 ensure_ascii
如果设置为 False
应该使函数不转义字符。
If ensure_ascii is true (the default), the output is guaranteed to have all incoming non-ASCII characters escaped. If ensure_ascii is false, these characters will be output as-is.