将utf-8文本保存为UTF8,而不是\ u转义序列

示例代码:

>>> import json >>> json_string = json.dumps("ברי צקלה") >>> print json_string "\u05d1\u05e8\u05d9 \u05e6\u05e7\u05dc\u05d4" 

问题是:这不是人类可读的。 我的(聪明的)用户想要使用JSON转储validation甚至编辑文本文件。 (我宁愿不使用XML)

有没有办法将对象序列化为utf-8 jsonstring(而不是\ uXXXX)?

这没有帮助:

 >>> output = json_string.decode('string-escape') "\u05d1\u05e8\u05d9 \u05e6\u05e7\u05dc\u05d4" 

工作,但如果任何子对象是一个python-unicode而不是utf-8,它会转储垃圾:

 >>> #### ok: >>> s= json.dumps( "ברי צקלה", ensure_ascii=False) >>> print json.loads(s) ברי צקלה >>> #### NOT ok: >>> d={ 1: "ברי צקלה", 2: u"ברי צקלה" } >>> print d {1: '\xd7\x91\xd7\xa8\xd7\x99 \xd7\xa6\xd7\xa7\xd7\x9c\xd7\x94', 2: u'\xd7\x91\xd7\xa8\xd7\x99 \xd7\xa6\xd7\xa7\xd7\x9c\xd7\x94'} >>> s = json.dumps( d, ensure_ascii=False, encoding='utf8') >>> print json.loads(s)['1'] ברי צקלה >>> print json.loads(s)['2'] ××¨× ×¦×§×× 

我search了json.dumps文档,但找不到有用的东西。

编辑 – 解决scheme(?):

我会尽量总结Martijn Pieters的评论和回答:

(编辑:@ Sebastian的评论和大约一年之后的第二个想法)

  1. json.dumps中可能没有内置的解决scheme。

  2. 我将不得不将所有的string转换为UTF8 Unicode的对象之前它是JSON编辑。 我将使用马克的函数,可以在嵌套对象中可靠地转换string

  3. 我给出的例子在很大程度上取决于我的计算机和IDE环境,而且在所有计算机上运行的都不一样。

谢谢大家 :)

使用ensure_ascii=False开关到json.dumps() ,然后手动将值编码为UTF-8:

 >>> json_string = json.dumps(u"ברי צקלה", ensure_ascii=False).encode('utf8') >>> json_string '"\xd7\x91\xd7\xa8\xd7\x99 \xd7\xa6\xd7\xa7\xd7\x9c\xd7\x94"' >>> print json_string "ברי צקלה" 

如果您正在将其写入文件,则可以使用io.open()而不是open()来生成一个文件对象,在编写时为您编码Unicode值,然后使用json.dump()写入该文件:

 with io.open('filename', 'w', encoding='utf8') as json_file: json.dump(u"ברי צקלה", json_file, ensure_ascii=False) 

在Python 3中,内置的open()io.open()的别名。 请注意, 在json模块中有一个bug,其中ensure_ascii=False标志可以产生unicodestr对象的混合 。 Python 2的解决方法是:

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

如果您传递的编码为UTF-8的字节string(Python 2中的str ,Python 3中的bytes ),请确保也设置encoding关键字:

 >>> d={ 1: "ברי צקלה", 2: u"ברי צקלה" } >>> d {1: '\xd7\x91\xd7\xa8\xd7\x99 \xd7\xa6\xd7\xa7\xd7\x9c\xd7\x94', 2: u'\u05d1\u05e8\u05d9 \u05e6\u05e7\u05dc\u05d4'} >>> s=json.dumps(d, ensure_ascii=False, encoding='utf8') >>> s u'{"1": "\u05d1\u05e8\u05d9 \u05e6\u05e7\u05dc\u05d4", "2": "\u05d1\u05e8\u05d9 \u05e6\u05e7\u05dc\u05d4"}' >>> json.loads(s)['1'] u'\u05d1\u05e8\u05d9 \u05e6\u05e7\u05dc\u05d4' >>> json.loads(s)['2'] u'\u05d1\u05e8\u05d9 \u05e6\u05e7\u05dc\u05d4' >>> print json.loads(s)['1'] ברי צקלה >>> print json.loads(s)['2'] ברי צקלה 

请注意, 您的第二个示例是无效的Unicode; 你把UTF-8字节作为一个Unicode字面量,这将永远不会工作:

 >>> s = u'\xd7\x91\xd7\xa8\xd7\x99 \xd7\xa6\xd7\xa7\xd7\x9c\xd7\x94' >>> print s ××¨× ×¦×§×× >>> print s.encode('latin1').decode('utf8') ברי צקלה 

只有当我把这个string编码为拉丁语1(Unicode字符的Unicode码映射一对一),然后解码为UTF-8才能看到预期的输出。 这与JSON无关,一切与你使用错误的input。 结果被称为Mojibake 。

如果您从string文本中获得该Unicode值,则会使用错误的编码解码器对其进行解码。 这可能是你的terminalconfiguration错误,或者你的文本编辑器使用不同于你告诉Python读取文件的编解码器来保存你的源代码。 或者你从一个应用了错误编解码器的库中获取它。 这一切都与JSON库无关

更新:这是错误的答案,但它仍然是有用的理解为什么它是错的。 看评论。

如何unicode-escape

 >>> d = {1: "ברי צקלה", 2: u"ברי צקלה"} >>> json_str = json.dumps(d).decode('unicode-escape').encode('utf8') >>> print json_str {"1": "ברי צקלה", "2": "ברי צקלה"} 

Peters的python 2解决方法在边缘情况下失败:

 d = {u'keyword': u'bad credit \xe7redit cards'} with io.open('filename', 'w', encoding='utf8') as json_file: data = json.dumps(d, ensure_ascii=False).decode('utf8') try: json_file.write(data) except TypeError: # Decode data to Unicode first json_file.write(data.decode('utf8')) UnicodeEncodeError: 'ascii' codec can't encode character u'\xe7' in position 25: ordinal not in range(128) 

它崩溃在第3行的.decode('utf8')部分。我通过避免这个步骤以及ascii的特殊框架来使程序变得更简单,从而解决了这个问题:

 with io.open('filename', 'w', encoding='utf8') as json_file: data = json.dumps(d, ensure_ascii=False, encoding='utf8') json_file.write(unicode(data)) cat filename {"keyword": "bad credit çredit cards"} 

容易像一个蛋糕

写入文件

 import codecs import json with codecs.open('your_file.txt', 'w', encoding='utf-8') as f: json.dump({"message":"xin chào việt nam"}, f, ensure_ascii=False) 

打印到标准input

 import codecs import json print(json.dumps({"message":"xin chào việt nam"}, ensure_ascii=False)) 

这是我的解决scheme使用json.dump():

 def jsonWrite(p, pyobj, ensure_ascii=False, encoding=SYSTEM_ENCODING, **kwargs): with codecs.open(p, 'wb', 'utf_8') as fileobj: json.dump(pyobj, fileobj, ensure_ascii=ensure_ascii,encoding=encoding, **kwargs) 

SYSTEM_ENCODING设置为:

 locale.setlocale(locale.LC_ALL, '') SYSTEM_ENCODING = locale.getlocale()[1] 

以下是我的理解VAR阅读上面的答案和谷歌。

 # coding:utf-8 r""" @update: 2017-01-09 14:44:39 @explain: str, unicode, bytes in python2to3 #python2 UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 7: ordinal not in range(128) #1.reload #importlib,sys #importlib.reload(sys) #sys.setdefaultencoding('utf-8') #python3 don't have this attribute. #not suggest even in python2 #see:http://stackoverflow.com/questions/3828723/why-should-we-not-use-sys-setdefaultencodingutf-8-in-a-py-script #2.overwrite /usr/lib/python2.7/sitecustomize.py or (sitecustomize.py and PYTHONPATH=".:$PYTHONPATH" python) #too complex #3.control by your own (best) #==> all string must be unicode like python3 (u'xx'|b'xx'.encode('utf-8')) (unicode 's disappeared in python3) #see: http://blog.ernest.me/post/python-setdefaultencoding-unicode-bytes #how to Saving utf-8 texts in json.dumps as UTF8, not as \u escape sequence #http://stackoverflow.com/questions/18337407/saving-utf-8-texts-in-json-dumps-as-utf8-not-as-u-escape-sequence """ from __future__ import print_function import json a = {"b": u"中文"} # add u for python2 compatibility print('%r' % a) print('%r' % json.dumps(a)) print('%r' % (json.dumps(a).encode('utf8'))) a = {"b": u"中文"} print('%r' % json.dumps(a, ensure_ascii=False)) print('%r' % (json.dumps(a, ensure_ascii=False).encode('utf8'))) # print(a.encode('utf8')) #AttributeError: 'dict' object has no attribute 'encode' print('') # python2:bytes=str; python3:bytes b = a['b'].encode('utf-8') print('%r' % b) print('%r' % b.decode("utf-8")) print('') # python2:unicode; python3:str=unicode c = b.decode('utf-8') print('%r' % c) print('%r' % c.encode('utf-8')) """ #python2 {'b': u'\u4e2d\u6587'} '{"b": "\\u4e2d\\u6587"}' '{"b": "\\u4e2d\\u6587"}' u'{"b": "\u4e2d\u6587"}' '{"b": "\xe4\xb8\xad\xe6\x96\x87"}' '\xe4\xb8\xad\xe6\x96\x87' u'\u4e2d\u6587' u'\u4e2d\u6587' '\xe4\xb8\xad\xe6\x96\x87' #python3 {'b': '中文'} '{"b": "\\u4e2d\\u6587"}' b'{"b": "\\u4e2d\\u6587"}' '{"b": "中文"}' b'{"b": "\xe4\xb8\xad\xe6\x96\x87"}' b'\xe4\xb8\xad\xe6\x96\x87' '中文' '中文' b'\xe4\xb8\xad\xe6\x96\x87' """ 

正如Martijn所指出的,在json.dumps中使用ensure_ascii = False是解决这个问题的正确方向。 但是,这可能会引发一个例外:

 UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 1: ordinal not in range(128) 

你需要额外的设置在site.py或sitecustomize.py来设置你的sys.getdefaultencoding()正确。 site.py在lib / python2.7 /下,而sitecustomize.py在lib / python2.7 / site-packages下。

如果你想使用site.py,在def setencoding()下:如果0:改为1:这样python就会使用你的操作系统的语言环境。

如果你喜欢使用sitecustomize.py,如果你还没有创build它,那么它可能不存在。 简单地把这些线:

 import sys reload(sys) sys.setdefaultencoding('utf-8') 

那么你可以用UTF-8格式做一些中文的json输出,比如:

 name = {"last_name": u"王"} json.dumps(name, ensure_ascii=False) 

你会得到一个utf-8编码的string,而不是\ u转义jsonstring。

要validation您的默认编码:

 print sys.getdefaultencoding() 

你应该得到“UTF-8”或“UTF-8”来validation你的site.py或sitecustomize.py设置。

请注意,您无法在交互式Python控制台上执行sys.setdefaultencoding(“utf-8”)。