ChatGPT解决这个技术问题 Extra ChatGPT

将带有 json.dumps 的 utf-8 文本保存为 UTF8,而不是 \u 转义序列

示例代码:

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

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

有没有办法将对象序列化为 UTF-8 JSON 字符串(而不是 \uXXXX)?


M
Martijn Pieters

使用 ensure_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() 并将其留给文件对象进行编码:

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

Python 2 的注意事项

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

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

请注意,有一个 bug in the json module,其中 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))

在 Python 2 中,当使用编码为 UTF-8 的字节字符串(类型 str)时,请确保同时设置 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']
ברי צקלה

往返 encode/decode 似乎没有必要。只需设置 ensure_ascii=False(根据 this answer)似乎就足够了。
@AdamAL 请更彻底地阅读我的答案:这个答案中没有往返,除了一个解码调用,它只是用来证明字节值确实包含 UTF-8 编码数据。我的答案中的第二个代码片段直接写入文件,仅设置 ensure_ascii=False。注意:我强烈建议不要使用 codecs.open() 函数;该库早于 io 并且流实现有很多未解决的问题。
ensure_ascii=False - 在我的情况下就像一个魅力。我的用途:json.dumps(unicode_raw_dict, indent=2, ensure_ascii=False)
T
Tansc

写入文件

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

SyntaxError:第 5 行文件 json-utf8.py 中的非 ASCII 字符“\xc3”,但未声明编码;有关详细信息,请参见 python.org/dev/peps/pep-0263
谢谢!我没想到这么简单。如果您要转换为 json 的数据是不受信任的用户输入,您只需要小心。
仅使用 codecs 库对我有用。谢谢!
@igorkf 如果只有 codecs.open() 在内置 open() 失败的地方工作,那将是非常令人惊讶的。您可能在使用 Python 2 吗?
很久以前了,不过我用的是python3.7或者3.8
B
BBog

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

“边缘案例”对我来说只是一个愚蠢的未经测试的错误。与使用异常处理相比,您的 unicode(data) 方法是更好的选择。请注意,encoding='utf8' 关键字参数与 json.dumps() 产生的输出无关;它用于解码函数接收的 str 输入
@MartijnPieters:或更简单:open('filename', 'wb').write(json.dumps(d, ensure_ascii=False).encode('utf8')) 无论 dumps 返回(仅 ascii)str 还是 unicode 对象,它都有效。
@JFSebastian:对,因为 str.encode('utf8') 解码 首先隐式。但如果给定 str 对象,unicode(data) 也是如此。 :-) 使用 io.open() 为您提供了更多选择,包括使用编写 BOM 的编解码器,并且您正在使用其他内容跟踪 JSON 数据。
@MartijnPieters:基于 .encode('utf8') 的变体适用于 Python 2 和 3(相同的代码)。 Python 3 上没有 unicode。无关:json 文件不应使用 BOM(尽管确认 json 解析器可能会忽略 BOM,请参阅 errate 3983)。
encoding='utf8' 添加到 json.dumps 可以解决问题。 PS我有一个西里尔文字要转储
m
monitorius

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

unicode-escape 怎么样?

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

unicode-escape 不是必需的:您可以改用 json.dumps(d, ensure_ascii=False).encode('utf8')。并且不能保证 json 在 所有 情况下使用与 Python 中的 unicode-escape 编解码器完全相同 的规则,即,结果在某些角落可能相同也可能不同案子。否决票是针对不必要且可能错误的转换。不相关:print json_str 仅适用于 utf8 语言环境,或者如果 PYTHONIOENCODING envvar 在此处指定 utf8(改为打印 Unicode)。
另一个问题:字符串值中的任何双引号都将失去转义,因此这将导致 JSON 输出损坏。
Python3 中的错误:AttributeError:“str”对象没有属性“decode”
unicode-escape 工作正常!我会接受这个答案作为正确答案。
@jfs 不,至少对我来说,json.dumps(d, ensure_ascii=False).encode('utf8') 不起作用。我收到 UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position ...-错误。但是,unicode-escape 变体可以正常工作。
N
Nik

从 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": "ƒ"}

也在 python 3.6 中(刚刚验证)。
C
Cheney

以下是我的理解 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'
"""

s
sivi

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

print(json.dumps(result_dict,ensure_ascii=False))

没关系。如果不是必须的,请考虑尝试在代码中不要写太多文本。

这对于 python 控制台来说可能已经足够好了。但是,为了满足服务器的要求,您可能需要按照此处的说明设置区域设置(如果它在 apache2 上)http://blog.dscpl.com.au/2014/09/setting-lang-and-lcall-when-using.html

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

locale -a 

在 XX 是你的语言的地方安装它

sudo apt-get install language-pack-XX

例如:

sudo apt-get install language-pack-he

将以下文本添加到 /etc/apache2/envvrs

export LANG='he_IL.UTF-8'
export LC_ALL='he_IL.UTF-8'

比你希望不会从 apache 得到 python 错误,比如:

print (js) UnicodeEncodeError: 'ascii' codec can't encode characters in position 41-45: ordinal not in range(128)

同样在 apache 中尝试将 utf 设置为默认编码,如下所述:
How to change the default encoding to UTF-8 for Apache?

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


N
Neit Sabes

这是我使用 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]

Y
Yulin GUO

尽可能使用编解码器,

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

C
Chandan Sharma

如果您从文件和文件内容阿拉伯文本加载 JSON 字符串。然后这将起作用。

假设文件如:arabic.json

{ 
"key1" : "لمستخدمين",
"key2" : "إضافة مستخدم"
}

从arabic.json 文件中获取阿拉伯语内容

with open(arabic.json, encoding='utf-8') as f:
   # deserialises it
   json_data = json.load(f)
   f.close()


# json formatted string
json_data2 = json.dumps(json_data, ensure_ascii = False)

要在 Django 模板中使用 JSON 数据,请执行以下步骤:

# If have to get the JSON index in Django Template file, then simply decode the encoded string.

json.JSONDecoder().decode(json_data2)

完毕!现在我们可以将结果作为带有阿拉伯值的 JSON 索引。


fh.close() fh 未定义。
现在已更正。这将是 f.close()
C
ChrisXiao

使用 unicode-escape 解决问题

>>>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: 漢  χαν  хан

原始资源:https://blog.csdn.net/chuatony/article/details/72628868


使用库的内置功能 ensure_ascii=False 而不是自己滚动是愚蠢的。 (但请理解,将 JSON 保存为纯 UTF-8 会引入互操作性问题,尤其是在 Windows 上。)
@tripleee 它并不傻 - 它是唯一给出确切结果的解决方案,它与使用 utf-8 的文件写入类似的编码。
R
Ryan X

正如 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(): 下将第一个 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") 。


不。不要这样做。修改默认字符编码与 jsonensure_ascii=False 无关。如果您不这么认为,请提供一个最小的完整代码示例。
只有当您输入非 ASCII 字节字符串(例如,非 Unicode 值)或尝试将生成的 JSON 值(Unicode 字符串)与非 ASCII 字节字符串组合时,才会出现此异常。如果您没有正确管理字符串数据,则将默认编码设置为 UTF-8 本质上是掩盖了一个潜在问题。