美文网首页
python json.dumps 参数 ensure_asc

python json.dumps 参数 ensure_asc

作者: Joncc | 来源:发表于2021-07-08 11:05 被阅读0次

ensure_ascii

json.dumps 序列化时对中文默认使用的ascii编码.想输出中文需要指定ensure_ascii=False(此时编码为utf-8):

>>> import json
>>> sstr = json.dumps("你好")
>>> print(sstr)
"\u4f60\u597d"
>>> ustr = json.dumps("你好", ensure_ascii=False)
>>> print(ustr)
"你好"

indent

indent:参数根据数据格式缩进显示,读起来更加清晰。

>>> import json
>>> x = {'name':'jon','age':17,'city':'shanghai'}
>>> y = json.dumps(x) #用dumps将python编码成json字符串
>>> print(y)
{"city": "shanghai", "age": 17, "name": "jon"}
>>> z = json.dumps(x, indent=2)
>>> print(z)
{
  "city": "shanghai", 
  "age": 17, 
  "name": "jon"
}

相关文章

网友评论

      本文标题:python json.dumps 参数 ensure_asc

      本文链接:https://www.haomeiwen.com/subject/cugzultx.html