Python json.ensure_ascii 参考
Python json.ensure_ascii 参考,整理标准库调用语法、参数、示例、返回值和常见错误。
语法
import json
text = json.dumps(data, ensure_ascii=False)
参数
text / file:JSON 字符串或已打开的文件对象。ensure_ascii / indent:控制中文输出和缩进格式。
返回值
返回解析后的 dict/list,或把对象写入字符串/文件;格式错误会抛出 JSONDecodeError。
示例
import json
print(json.dumps({"name": "菜鸟教程"}, ensure_ascii=False))
常见错误
- JSON 字符串必须使用双引号,不能直接解析 Python 字典写法。
- 写中文时没有设置
ensure_ascii=False,输出会变成 Unicode 转义。
相关内容
相关内容
- Python collections.ChainMap 参考 Python collections.ChainMap 参考,整理标准库调用语法、参数、示例、返回值和常见错误。
- Python collections.Counter 参考 Python collections.Counter 参考,整理标准库调用语法、参数、示例、返回值和常见错误。
- Python collections.defaultdict 参考 Python collections.defaultdict 参考,整理标准库调用语法、参数、示例、返回值和常见错误。
- Python collections.deque 参考 Python collections.deque 参考,整理标准库调用语法、参数、示例、返回值和常见错误。
- Python collections.most_common 参考 Python collections.most_common 参考,整理标准库调用语法、参数、示例、返回值和常见错误。
- Python collections.namedtuple 参考 Python collections.namedtuple 参考,整理标准库调用语法、参数、示例、返回值和常见错误。