jsonjson
json 序列化dumps以后,数据会变成很长的一行,若是,数据量很是大就会至关不易查看,使用indent
参数来输出便于查看的JSON。编码
如:spa
{ "终点站": "贵阳站", "车号": "K607", "日期": "2016年01月2日", "金额": "278.5", "座位号": "18车063号", "座位类型": "新空调硬座"}
且序列化以后默认为ascii格式,不便于查看,咱们禁用ascii编码转化为utf-8编码。code
小技巧以下:utf-8
jsonstr = json.dumps(str,indent=2,ensure_ascii=False)ci
jsonstr.encode(utf-8)it
{
"终点站": "贵阳站",
"车号": "K607",
"日期": "2016年01月2日",
"金额": "278.5",
"座位号": "18车063号",
"座位类型": "新空调硬座"
}
写入文件时也能够在打开文件时注明编码格式:coding
jsonstr = json.dumps(str,indent=2,ensure_ascii=False)技巧
with open('jsonstr.txt','a',encoding='utf-8') as f:序列化
f.write(jsonstr)