python json.dumps 中文

我们知道python 中json.dumps()函数用于将一个Python数据类型列表进行json格式的编码,换句话说json.dumps()函数是将字典转化为字符串。

比如:

import json
dic = {"name":"James"}
print(json.dumps(dic))

但是如果dict中包含中文,输出的str就是包含乱码的字符串。

如:

import json


dic = {"name":"哈哈"}
print(json.dumps(dic))


'''
输出:
{"name": "\u54c8\u54c8"}
'''

如果想要其输出中文,则需要添加参数ensure_ascii=False



dic = {"name":"哈哈"}
print(json.dumps(dic))

print(json.dumps(dic, ensure_ascii=False))


'''
{"name": "\u54c8\u54c8"}
{"name": "哈哈"}
'''
ensure_ascii参数的含义:
If ``ensure_ascii`` is false, then the return value can contain non-ASCII
characters if they appear in strings contained in ``obj``. Otherwise, all
such characters are escaped in JSON strings.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值