python3 json数据包含中文的读写问题
来源:互联网 发布:网络词搬砖是什么意思 编辑:程序博客网 时间:2024/05/21 06:12
python3 默认的是UTF-8格式,但在在用dump写入的时候仍然要注意:如下
import jsondata1 = { "TestId": "testcase001", "Method": "post", "Title": "登录测试", "Desc": "登录基准测试", "Url": "http://xxx.xxx.xxx.xx", "InputArg": { "username": "王小丫", "passwd": "123456", }, "Result": { "errorno": "0" }}with open('casedate.json', 'w', encoding='utf-8') as f: json.dump(data1, f, sort_keys=True, indent=4)
在打开文件的时候要加上encoding=‘utf-8’,不然会显示成乱码,如下:
{ "Desc": "��¼������", "InputArg": { "passwd": "123456", "username": "��СѾ" }, "Method": "post", "Result": { "errorno": "0" }, "TestId": "testcase001", "Title": "��¼����", "Url": "http://xxx.xxx.xxx.xx"}
在dump的时候也加上ensure_ascii=False,不然会变成ascii码写到文件中,如下:
{ "Desc": "\u767b\u5f55\u57fa\u51c6\u6d4b\u8bd5", "InputArg": { "passwd": "123456", "username": "\u738b\u5c0f\u4e2b" }, "Method": "post", "Result": { "errorno": "0" }, "TestId": "testcase001", "Title": "\u767b\u5f55\u6d4b\u8bd5", "Url": "http://xxx.xxx.xxx.xx"}
另外python3在向txt文件写中文的时候也要注意在打开的时候加上encoding=‘utf-8’,不然也是乱码,如下:
with open('result.txt', 'a+', encoding='utf-8') as rst: rst.write('return data') rst.write('|') for x in r.items(): rst.write(x[0]) rst.write(':')
0 0
- python3 json数据包含中文的读写问题
- 【Python3.5】读写JSON格式的数据
- python 读写Json的中文编码问题
- 读写json中文ASCII乱码的问题
- python3 urlopen打开包含中文的url
- Java读写json格式的txt文件中文乱码问题
- JSON数据的中文乱码问题
- json数据的中文显示问题 python
- 解决JSON数据的中文乱码问题
- JSON返回的数据中文乱码问题!!!
- Python3 中文文件读写
- [json] -- php返回json数据中文显示的问题
- JavaCV读写包含中文路径的图片
- 解决sencha touch显示.JSON包含中文数据时显示乱码问题
- python3读写csv数据
- 让Jackson JSON生成的数据包含的中文以unicode方式编码
- 让Jackson JSON生成的数据包含的中文以unicode方式编码
- 关于客户端与服务器ajax使用json数据交互,出现中文乱码或因包含html标答而报错的问题(base64加密)
- Video4Linux框架简介(1)
- DOS下PQ分区
- 链接选项修改解析----驱动开发
- 浅析ThreadLocal
- 当pandas.Series存在混合类型index的时候取子集出错的处理
- python3 json数据包含中文的读写问题
- 蓝牙低功耗(BLE)
- windows定时计划任务,定时执行数据库的备份任务
- 2017-02-08 UML 实用技术介绍
- 定义
- 树形DP
- Android动画——逐帧动画(Frame Animation)
- 李彦宏内部讲话释放的几点信号
- 如何在Eclipse中设置Java JDK源代码和文档的路径