美文网首页
[python格式化] [python 编码]

[python格式化] [python 编码]

作者: 阳春是你 | 来源:发表于2017-02-16 18:49 被阅读62次

在shell中,可以使用下面的方法格式化json
echo 'jsonstr' |python -m json.tool

在脚本中,可以这样写

json.loads()
json.dumps(obj_json,indent=4, ensure_ascii=False).encode('utf-8')

加indent=4(indent:缩进)参数吗,可以让json格式化显示,.encode('utf-8')是可以显示中文

python的编码

python 2.x默认的编码是ascii

import sys
print sys.getdefaultencoding()

可以使用一下代码将其设置为utf-8

sys.setdefaultencoding('utf-8')

在3.x中,默认的是utf-8

相关文章

网友评论

      本文标题:[python格式化] [python 编码]

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