gpt4 book ai didi

python - 为什么 json.dumps 使用 "\uxxxx"转义非 ascii 字符

转载 作者:太空宇宙 更新时间:2023-11-03 12:58:15 26 4
gpt4 key购买 nike

<分区>

在 Python 2 中,函数 json.dumps() 将确保所有非 ascii 字符都转义为 \uxxxx

Python 2 Json

但这不是很令人困惑吗,因为 \uxxxx 是一个 unicode 字符,应该在 unicode 字符串中使用。

json.dumps() 的输出是一个 str,它在 Python 2 中是一个字节字符串。因此它不应该将字符转义为 \xhh

>>> unicode_string = u"\u00f8"
>>> print unicode_string
ø
>>> print json.dumps(unicode_string)
"\u00f8"
>>> unicode_string.encode("utf8")
'\xc3\xb8'

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com