我能够成功将csv转换为json。我也试图将数据上传到ElasticSearch,因此格式略有不同,这就是为什么我需要将其转换为不同类型的json格式。
数据:
id,team_name,team_members
123,"Biology, Neurobiology ","Ali Smith, Jon Doe"
234,Mathematics,Jane Smith
345,"Statistics, Probability","Matt P, Albert Shaw"
456,Chemistry,"Andrew M, Matt Shaw, Ali Smith"
678,Physics,"Joe Doe, Jane Smith, Ali Smith "
代码:
import csv
import sys
import json
#EDIT THIS LIST WITH YOUR REQUIRED JSON KEY NAMES
fieldnames=["id","team_name","team_members"]
def convert(filename):
csv_filename = filename[0]
print "Opening CSV file: ",csv_filename
f=open(csv_filename, 'r')
next(f) #skip the headers
csv_reader = csv.DictReader(f,fieldnames)
json_filename = csv_filename.split(".")[0]+".json"
print "Saving JSON to file: ",json_filename
jsonf = open(json_filename,'w')
data = json.dumps([r for r in csv_reader])
jsonf.write(data)
f.close()
jsonf.close()
if __name__=="__main__":
convert(sys.argv[1:])
# How to run?: python csvtojsonfile.py myCSVfile.csv
当前输出:
[{"team_name": "Biology, Neurobiology ", "team_members": "Ali Smith, Jon Doe", "id": "123"}, {"team_name": "Mathematics", "team_members": "Jane Smith ", "id": "234"}, {"team_name": "Statistics, Probability", "team_members": "Matt P, Albert Shaw", "id": "345"}, {"team_name": "Chemistry", "team_members": "Andrew M, Matt Shaw, Ali Smith", "id": "456"}, {"team_name": "Physics", "team_members": "Joe Doe, Jane Smith, Ali Smith ", "id": "678"}]
需要最终产出:
{"index" : {}}
{"team_name": "Biology, Neurobiology ", "team_members": "Ali Smith, Jon Doe", "id": "123"}
{"index" : {}}
{"team_name": "Mathematics", "team_members": "Jane Smith ", "id": "234"}
{"index" : {}}
{"team_name": "Statistics, Probability", "team_members": "Matt P, Albert Shaw", "id": "345"}
{"index" : {}}
{"team_name": "Chemistry", "team_members": "Andrew M, Matt Shaw, Ali Smith", "id": "456"}
{"index" : {}}
{"team_name": "Physics", "team_members": "Joe Doe, Jane Smith, Ali Smith ", "id": "678"}
注意:我删除了逗号,创建了' {" index" :{}}',并创建了新行。我很感激帮助!
答案 0 :(得分:0)
如果你想打印它就像你可以循环遍历列表
for i in data:
print("index {}")
print(i)
如果你希望它在文件中看起来像你可以使用转储参数来改变外观,你就不能删除" []"。
json.dump(jsonf, [r for r in csv_reader], indent=2)# this will output to a file, you don't need a .write()