使用phython将csv转换为json

时间:2017-11-29 02:26:58

标签: json

这是我的代码,它成功转换。但是,当我将此json导入firebase并指出无效的JSON文件时。

import csv
import json

csvfile = open('C:/Users/Senior/seaborn-data/Denver DatasetCleaning Finalize.csv', 'r')
jsonfile = open('C:/Users/Senior/seaborn-data/Denver DatasetCleaning Finalize.json', 'w')

fieldnames = ("OFFENSE_CODE ","OFFENSE_CATEGORY_ID","FIRST_OCCURRENCE_DATE","DATE","YEAR","MONTH","DAY","TIME","HOUR","MINUTE","INCIDENT_ADDRESS","GEO_LON","GEO_LAT","NEIGHBORHOOD_ID")


reader = csv.DictReader( csvfile, fieldnames)
for row in reader:
    json.dump(row, jsonfile)
    jsonfile.write('\n')

1 个答案:

答案 0 :(得分:1)

每次调用json.dump时都会输出json。但是连接在一起的几个json字符串仍然不是json

您可能想要做的是将整个csv读入变量,然后json.dump