[英]Python JSON encoding issues
我需要解析下载的JSON文件
文件的URL
我得到unicode:
{u'type': u'string', u'name': u'Podla\u017e\xed', u'value': u'2. podla\u017e\xed'}
我需要转换:
\\ u017e至ž
\\ x固定到í
等等...
我该怎么办?
使用unicode时,必须确保在程序中使用输入之前正确解码输入,并在将其序列化为字节时将其编码回UTF-8
。 似乎您已经处理了前者,因此可以按原样使用字典。
当您想再次将dict另存为json时,必须指定正确的编码并将其序列化为UTF-8
。
import json
from io import open
with open('some_file.json', 'w', encoding='utf-8') as f:
f.write(json.dumps(some_dict, ensure_ascii=False))
您可以尝试使用代码打开。 参考: https : //docs.python.org/2/library/codecs.html
示例代码。
import codecs
fileObj = codecs.open( "someFile", "r", "utf-8" )
u = fileObj.read()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.