[英]Python Saving JSON Files as UTF-8
I'm trying to output some UTF-8 characters to a JSON file. 我正在尝试将一些UTF-8字符输出到JSON文件。
When I save the file they're being written like this: 当我保存文件时,它们的编写方式如下:
{"some_key": "Enviar invitaci\ón privada"} {“some_key”:“Enviar invitaci \\ u00f3n privada”}
The above is valid and works. 以上内容有效且有效。 When I load the file and print 'some_key' it displays "Enviar invitación privada" in the terminal.
当我加载文件并打印'some_key'时,它会在终端显示“Enviartunitaciónprivada”。
Is there anyway to write the JSON file with "some_key" as the encoded version, like this? 反正有没有用“some_key”作为编码版本编写JSON文件,像这样?
{"some_key": "Enviar invitación privada"} {“some_key”:“Enviarinvitaciónprivada”}
Set ensure_ascii
to False
: 将
ensure_ascii
设置为False
:
>>> print json.dumps(x, ensure_ascii=False)
{"some_key": "Enviar invitación privada"}
Using Python 3.4.3 here and using dump()
instead of dumps()
: 在这里使用Python 3.4.3并使用
dump()
而不是dumps()
:
with open("example.json","w", encoding='utf-8') as jsonfile:
json.dump(data,jsonfile,ensure_ascii=False)
is the standard way to write JSON to an UTF-8 encoded file. 是将JSON写入UTF-8编码文件的标准方法。
If you want the escaped code points instead of the characters in your file, set ensure_ascii=True
. 如果您想要转义的代码点而不是文件中的字符,请设置
ensure_ascii=True
。 This writes for example the Euro-character €
as \€
directly to your file. 这会将例如欧元字符
€
作为\€
直接写入您的文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.