把一个json文件写入到csv文件当中【多测师_王sir】
把如下的example.json文件写进csv文件当中
{ "01": [ { "picname": "01_sseye.png", "主观评分": "7", "主观评价": "ok" }, { "picname": "01_ueeye.png", "主观评分": "8", "主观评价": "眼球" } ], "02": [ { "picname": "02_sshair.png", "主观评分": "5", "主观评价": "ok" }, { "picname": "02_uehair.png", "主观评分": "9", "主观评价": "ok" } ], "03": [ { "picname": "03_ssll.png", "主观评分": "6", "主观评价": "" }, { "picname": "03_uesll.png", "主观评分": "6", "主观评价": "" } ] }
![](https://img2020.cnblogs.com/blog/1257390/202009/1257390-20200917172521096-12283488.png)
#coding=utf-8 """ =========================== Author:多测师_王sir Time:2020-09-17 11:53 Wechat:xiaoshubass website:www.duoceshi.cn =========================== """ import pandas as pd def read_example(path): data=pd.read_json(path) dict_data=data.to_dict() #转换为字典 list1=[] for i in dict_data.values(): for j in i.values(): list1.append(j) a=pd.DataFrame(list1) a.rename(columns={'picname':''},inplace=True) #picname改为空 #index = False 把第一列的索引值去掉 csv=a.to_csv('C:\project\dcs6\decorator\example.csv',encoding='utf-8',index=False) print(a) read_example('C:\project\dcs6\decorator\example.json')