Python json数据中文输出问题。

这个问题困扰了我好久好久,最后看了一眼官方文档,解决问题了。

问题描述:从web上获取的json数据,然后对应的保存到了python的类型中。再次输出这个数据时,中文总会变成\u1234这种形式。

Python版本:2.7

解决方案:使用json.dump(objfpskipkeys=Falseensure_ascii=Truecheck_circular=Trueallow_nan=Truecls=Noneindent=Noneseparators=None,encoding="utf-8"default=Nonesort_keys=False**kw)

注意ensure_ascii这个参数,官方文档说,当它为false是,就会把非ASCII字符转成\uxxxx序列的形式输出。所以只要调用的时候,改下参数就好了。

If ensure_ascii is True (the default), all non-ASCII characters in the output are escaped with \uXXXX sequences, and the result is astr instance consisting of ASCII characters only. If ensure_ascii is False, some chunks written to fp may be unicode instances. This usually happens because the input contains unicode strings or the encoding parameter is used. Unless fp.write() explicitly understands unicode (as in codecs.getwriter()) this is likely to cause an error.

 

 

json.dumps(your_json,ensure_ascii=False)

这货的另外一些功能就是按照key值排序,设置排版之类的。详情查看api:

http://docs.python.org/2/library/json.html

转载于:https://www.cnblogs.com/jzlikewei/p/3519487.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值