python3 json数据包含中文的读写问题
2017-02-08 17:38
555 查看
python3 默认的是UTF-8格式,但在在用dump写入的时候仍然要注意:如下
在打开文件的时候要加上encoding=‘utf-8’,不然会显示成乱码,如下:
在dump的时候也加上ensure_ascii=False,不然会变成ascii码写到文件中,如下:
另外python3在向txt文件写中文的时候也要注意在打开的时候加上encoding=‘utf-8’,不然也是乱码,如下:
python3 默认的是UTF-8格式,但在在用dump写入的时候仍然要注意:如下
import json data1 = { "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(':')
相关文章推荐
- 少见但实用的python模块集合
- 使用python的Paramiko模块登陆SSH
- 简单爬虫
- python数据分析与挖掘学习笔记(4)-垃圾邮件自动识别
- Python问题解决-让python默认启用python2(ubuntu)
- 几种python隐藏源码工具的用法
- python下载youtube视频
- The _imaging C module is not installed ---mark
- python爬虫04--有道翻译
- python实现post请求
- numpy 学习笔记(1)
- 详细记录python的range()函数用法
- Python IDE专用编辑器PyCharm下载及配置安装过程(Ubuntu环境)
- python类方法,实例方法,静态方法的区别
- Python Internet
- python自学笔记
- Python中的元组和列表的区别
- Python安装、入门、进阶的相关资源搜集
- scrapy 安装
- 使用python爬取全国所有热门景点数据---去哪儿网