标签:json als pre code 12px open 文件 write 空调
json
json 序列化dumps之后,数据会变成很长的一行,如果,数据量非常大就会相当不易查看,使用indent
参数来输出便于查看的JSON。
如:
{ "终点站": "贵阳站", "车号": "K607", "日期": "2016年01月2日", "金额": "278.5", "座位号": "18车063号", "座位类型": "新空调硬座"}
且序列化之后默认为ascii格式,不便于查看,我们禁用ascii编码转化为utf-8编码。
小技巧如下:
jsonstr = json.dumps(str,indent=2,ensure_ascii=False)
jsonstr.encode(utf-8)
{
"终点站": "贵阳站",
"车号": "K607",
"日期": "2016年01月2日",
"金额": "278.5",
"座位号": "18车063号",
"座位类型": "新空调硬座"
}
写入文件时也可以在打开文件时注明编码格式:
jsonstr = json.dumps(str,indent=2,ensure_ascii=False)
with open(‘jsonstr.txt‘,‘a‘,encoding=‘utf-8‘) as f:
f.write(jsonstr)
标签:json als pre code 12px open 文件 write 空调
原文地址:http://www.cnblogs.com/wzjbg/p/7422247.html