python csv转json格式

参考网上资料,原创地址忘记…

import csv
import json
import pandas as pd
csvfile = open('x.csv','r')#,encoding="gbk",errors='ignore'
jsonfile = open('x.json','w')
namesss= pd.read_csv('x.csv')
fieldnames1=namesss.columns
#type(fieldnames1)
#fieldnames = ("time","real_t","predict_t")
aaaa=tuple(fieldnames1)
reader = csv.DictReader(csvfile,aaaa)
for row in reader:
    json.dump(row,jsonfile)
    jsonfile.write('\n')
jsonfile.close()
csvfile.close()
print("succees!")

你可能感兴趣的:(Python)