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!")
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章