首页 > 解决方案 > 在python中使用json中的符号

问题描述


最近,我在 python 中使用 json 时遇到了问题。实际上这是关于 json 中的特殊符号。问题用下面的代码定义:

import json
app = {
       "text": "°"
       }
print(json.dumps(app, indent=2))

但给这个我得到这个:

{
  "text": "\u00b0"
}

这里的°符号被替换为\u00b0。但我希望它与我的输入完全相同。我该怎么做?

提前致谢。

标签: pythonjsonsymbols

解决方案


根据https://pynative.com/python-json-encode-unicode-and-non-ascii-characters-as-is/,您要设置ensure_ascii=False

>>> import json
>>> app={"text": "°"}
>>> print(json.dumps(app, indent=2, ensure_ascii=False))
{
  "text": "°"
}

推荐阅读