在 python 中使用 json 中的符号

using symbols in json in python


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

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

但是给出这个我得到这个:

{
  "text": "\u00b0"
}

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

提前致谢。

根据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": "°"
}