#coding=utf-8 import json,csv def csvTojson(a,b): global jsonlist jsonlist=[] with open(a,'r',encoding='utf-8') as fils: reader=csv.DictReader(fils) for i in reader: jsonlist.append(dict(i)) #读出是列表包字典 with open(b,'w',encoding='utf-8') as jsond: #写入是列表加字典文件 json.dump(jsonlist,jsond,ensure_ascii='') print(jsonlist) print('成功') if __name__ == '__main__': csvTojson('date.csv','tojson.json')
#coding=utf-8 import json,csv def jsonTocsv(paths,pathd): global filed with open(paths,'r',encoding='utf-8') as filed: #读入是列表 filed=json.load(filed) print(filed) with open(pathd,'w',encoding='utf-8',newline='') as csvfile: #字典写入模式 write=csv.DictWriter(csvfile,filed[0]) write.writeheader() for i in filed: write.writerow(i) print('成功') if __name__ == '__main__': jsonTocsv('date.json','tocsv.csv')
#coding=utf-8 import json,xlwt def jsonToexcel(paths,b): global filed with open(paths,'r',encoding='utf-8') as filed: filed=json.load(filed) print(filed) filepath='D:/untitled/1022toxls.xls' workbook=xlwt.Workbook() sheet=workbook.add_sheet('sheet1') #提取表头 写入表头 header=[i for i in filed[0]] for i in range(len(header)): sheet.write(0,i,header[i]) #遍历出json列表字典的值 global listdd listdd=[] for iss in filed: bb=[] for j in iss.values(): bb.append(j) listdd.append(bb) #写入值 for j in range(1,len(listdd)+1): for i in range(len(header)): sheet.write(j, i, listdd[j-1][i]) workbook.save(filepath) print('成功') if __name__ == '__main__': jsonToexcel('date.json','toxls.xls')