Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用json.dumps将utf-8文本保存为UTF8,而不是\u转义序列_Python_Json_Unicode_Utf 8_Escaping - Fatal编程技术网

Python 使用json.dumps将utf-8文本保存为UTF8,而不是\u转义序列

Python 使用json.dumps将utf-8文本保存为UTF8,而不是\u转义序列,python,json,unicode,utf-8,escaping,Python,Json,Unicode,Utf 8,Escaping,示例代码: >>> import json >>> json_string = json.dumps("ברי צקלה") >>> print(json_string) "\u05d1\u05e8\u05d9 \u05e6\u05e7\u05dc\u05d4" 问题是:它不是人类可读的。我的(智能)用户希望验证甚至编辑带有JSON转储的文本文件(我不希望使用XML) 是否有方法将对象序列化为UTF-

示例代码:

>>> import json
>>> json_string = json.dumps("ברי צקלה")
>>> print(json_string)
"\u05d1\u05e8\u05d9 \u05e6\u05e7\u05dc\u05d4"
问题是:它不是人类可读的。我的(智能)用户希望验证甚至编辑带有JSON转储的文本文件(我不希望使用XML)


是否有方法将对象序列化为UTF-8 JSON字符串(而不是
\uxxx
)?

使用
确保ascii=False
切换到
JSON.dumps()
,然后手动将值编码为UTF-8:

>>> 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())
"ברי צקלה"
如果要写入文件,只需使用
json.dump()
并将其留给file对象进行编码:

with open('filename', 'w', encoding='utf8') as json_file:
    json.dump("ברי צקלה", json_file, ensure_ascii=False)
Python 2的注意事项

对于Python2,还有一些需要考虑的注意事项。如果要将其写入文件,可以使用而不是
open()
来生成一个文件对象,该对象在写入时为您编码Unicode值,然后使用
json.dump()
来写入该文件:

with io.open('filename', 'w', encoding='utf8') as json_file:
    json.dump(u"ברי צקלה", json_file, ensure_ascii=False)
请注意,
sure_ascii=False
标志会产生
unicode
str
对象的混合。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))
在Python 2中,当使用编码为UTF-8的字节字符串(type
str
)时,请确保还设置了
编码
关键字:

>>> 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']
ברי צקלה

正如Martijn所指出的,在json.dumps中使用sure_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()下:将第一个If 0:更改为If 1:以便python使用操作系统的区域设置

如果您喜欢使用sitecustomize.py,如果您尚未创建它,它可能不存在。简而言之:

import sys
reload(sys)
sys.setdefaultencoding('utf-8')
然后您可以以utf-8格式进行一些中文json输出,例如:

name = {"last_name": u"王"}
json.dumps(name, ensure_ascii=False)
您将得到一个utf-8编码的字符串,而不是\u转义的json字符串

要验证默认编码,请执行以下操作:

print sys.getdefaultencoding()
您应该获得“utf-8”或“utf-8”以验证site.py或sitecustomize.py设置


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

更新:这是错误的答案,但理解其错误原因仍然很有用。见评论

unicode转义怎么样

>>> 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"}

下面是我使用json.dump()的解决方案:

其中,系统_编码设置为:

locale.setlocale(locale.LC_ALL, '')
SYSTEM_ENCODING = locale.getlocale()[1]
写入文件

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)
打印到标准输出

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

以下是我对以上答案和谷歌的理解

# 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'
"""

如果可能,使用编解码器

with codecs.open('file_path', 'a+', 'utf-8') as fp:
    fp.write(json.dumps(res, ensure_ascii=False))

从Python 3.7开始,以下代码可以正常工作:

from json import dumps
result = {"symbol": "ƒ"}
json_string = dumps(result, sort_keys=True, indent=2, ensure_ascii=False)
print(json_string)

输出:

{"symbol": "ƒ"}

如果您正在从文件加载JSON字符串&文件内容为阿拉伯语文本。这样就行了

假设文件为:arabic.json 从arabic.json文件获取阿拉伯语内容 要在Django模板中使用JSON数据,请执行以下步骤: 完成了!现在,我们可以将结果作为带有阿拉伯语值的JSON索引


谢谢你的原始答案。使用python 3以下代码行:

print(json.dumps(result_dict,ensure_ascii=False))
还好。考虑不要在代码中写太多的文本,如果不是命令的话。 这对于python控制台来说已经足够好了。但是,为了满足服务器的要求,您可能需要按照此处的说明设置语言环境(如果它位于apache2上)

基本上在ubuntu上安装he_IL或任何语言环境 检查它是否未安装

locale -a 
在XX是您的语言的地方安装它

sudo apt-get install language-pack-XX
例如:

sudo apt-get install language-pack-he
将以下文本添加到/etc/apache2/envrs

export LANG='he_IL.UTF-8'
export LC_ALL='he_IL.UTF-8'
希望您不会从apache上获得python错误,例如:

打印(js) UnicodeEncodeError:“ascii”编解码器无法对位置41-45中的字符进行编码:序号不在范围内(128)

同样在apache中,尝试将utf作为默认编码,如下所述:

尽早这样做,因为apache错误可能会很难调试,而且您可能会错误地认为它来自python,而在这种情况下,情况可能并非如此

使用unicode escape来解决问题 解释
原始资源:

不,不要这样做。修改默认字符编码与
json
确保ascii=False
无关。如果您不这么认为,请提供一个最小的完整代码示例。只有在输入非ASCII字节字符串(例如非Unicode值)或尝试将结果JSON值(Unicode字符串)与非ASCII字节字符串组合时,才会出现此异常。将默认编码设置为UTF-8实际上掩盖了一个潜在问题,即您没有正确管理字符串数据。“边缘大小写”只是我的一个未经测试的错误。您的
unicode(数据)
方法比使用异常处理更好。请注意,
encoding='utf8'
关键字参数与
json.dumps()
生成的输出无关;它用于解码函数接收的
str
输入。@MartijnPieters:或更简单的:
open('filename','wb')。write(json.dumps(d,确保ascii=False)。encode('utf8'))
无论
dumps
返回(仅ascii)str还是unicode对象,它都起作用。@J.F.Sebastian:对,因为
str.encode('utf8'))
首先隐式解码。但是,如果给定一个
str
对象,
unicode(数据)
,也是如此。:-)不过,使用
io.open()
可以提供更多选项,包括使用编写BOM表的编解码器,您可以使用其他内容跟踪JSON数据。@MartijnPieters:
.encode('utf8')
,基于此的变体可以在Python 2和3上工作(相同的代码)。Python3上没有
unicode
。无关:json文件不应使用BOM(尽管是一个确认json文件)
locale -a 
sudo apt-get install language-pack-XX
sudo apt-get install language-pack-he
export LANG='he_IL.UTF-8'
export LC_ALL='he_IL.UTF-8'
>>>import json
>>>json_string = json.dumps("ברי צקלה")
>>>json_string.encode('ascii').decode('unicode-escape')
'"ברי צקלה"'
>>>s = '漢  χαν  хан'
>>>print('unicode: ' + s.encode('unicode-escape').decode('utf-8'))
unicode: \u6f22  \u03c7\u03b1\u03bd  \u0445\u0430\u043d

>>>u = s.encode('unicode-escape').decode('utf-8')
>>>print('original: ' + u.encode("utf-8").decode('unicode-escape'))
original: 漢  χαν  хан