python - 在python中使用json中的符号
问题描述
最近,我在 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": "°"
}
推荐阅读
- c# - 如何在 Headless 模式下运行我的 Selenium 项目?
- linux - Docker 卷挂载为空
- php - 拉拉维尔 | 大量数据的特殊验证
- sql - 过滤条件的性能输出
- reactjs - React 新手,无法让 ReactDOM 渲染 javascript 组件
- perl - 如何在 Windows 10 上重新启动我的 Perl 脚本
- angular - 如何获得与我的 Table 对象的列名称相对应的正确数量的 formsControl,而无需努力编写
- python - 如何创建一系列单词以作为超参数进行迭代?
- azure-devops - Azure DevOps - 对“迭代”字段执行操作的新工作项规则
- html - 如何解析 HTML 正文以获取表格数据?