Python:删除[]字符,并添加新行

时间:2018-05-15 15:56:42

标签: json python-3.x csv elasticsearch

我能够成功将csv转换为json。我也试图将数据上传到ElasticSearch,因此格式略有不同,这就是为什么我需要将其转换为不同类型的json格式。

  1. 所以,我试图删除开始和结束' ['和']'来自json文件。
  2. 此外,我正在尝试插入' {" index" :{}}'对于数据行的每个新行。
  3. 创建像Pretty print
  4. 这样的新行

    数据:

    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" :{}}',并创建了新行。我很感激帮助!

1 个答案:

答案 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()