python 读写Json的中文编码问题
来源:互联网 发布:龙华新区行知实验小学 编辑:程序博客网 时间:2024/05/30 05:09
读写json文件:http://python3-cookbook.readthedocs.org/zh_CN/latest/c06/p02_read-write_json_data.html
首先 import json
load() loads()
dump() dumps()
一、从文件:
json转python数据结构:json.load
fo = open('data.json', 'r') data = json.load(fo)
python数据结构转json:json.dump
fo = open('data.json', 'r') json.dump(data, fo)
二、python字符串和json之间互相转换:
json_str = json.dumps(data)
data = json.loads(json_str)
三、json格式化输出:
1、格式化打印 pprint
2、格式化输出
在编码JSON的时候,还有一些选项很有用。 如果你想获得漂亮的格式化字符串后输出,可以使用json.dumps()
的indent参数。 它会使得输出和pprint()函数效果类似。比如:
print(json.dumps(data, indent=4))
四、带有中文的json转换:fo = open('data.json', 'r') json.dump(data, fo, indent=4)
jsondata= json.dumps( dics, ensure_ascii = False, indent = 4 )
在dumps方法中加入参数ensure_ascii = False,可以使dic中的中文正常转换
2、若python的数据中既有普通字符,又有Unicode字符串,上述方法则不行,还要在后面加上encode('utf-8')
手动转换成utf-8编码
jsondata= json.dumps( dics, ensure_ascii = False, indent = 4 ).encode('utf-8')
0 0
- python 读写Json的中文编码问题
- python中json数据中文编码显示的问题
- python json.dumps 中文编码问题
- Python读写Json涉及到中文的处理
- 读写json中文ASCII乱码的问题
- python的中文编码问题
- python的中文编码问题
- Python的中文编码问题
- python中文编码的问题
- Python 中文编码的问题
- Python的中文编码问题
- Python 文件读写,中文编码
- 使用 python 读写中文json
- 处理PHP的JSON中文编码问题
- 【python】python的中文编码问题
- python json.dumps 中文编码
- python json.dumps 中的ensure_ascii 参数引起的中文编码问题
- python 中文编码 问题
- 传参
- Mac 常用快捷键
- PHPExcel开发者文档[中文版]
- 【Axure学习】(二)Axure遮罩层
- Android LBS地图开发:地球地理GPS坐标系经纬度偏移偏差
- python 读写Json的中文编码问题
- 洗牌算法:随机打乱一个数组
- CRC校验算法学习(这个算法看了很多遍了,都是囫囵吞枣,这次将资料拷贝到这里,好好学习一下)
- Linux学习日记--基础命令(5)--用户和组概念与管理--文件权限
- eMMC CMD6的作用和使用方法
- STL
- 奔跑吧,我的JavaScript(3)
- SerDes interface参考设计_程序优化(8)
- 金融高富帅PK金融民工——职业规划方法论