Python - convert csv file to JSON

前端 未结 4 1391
醉梦人生
醉梦人生 2020-12-19 04:44

I need to convert a csv file into a hierarchical JSON object (preferably using Python). I thought that the script I have (below) does a correct job of converting to JSON, bu

4条回答
  •  难免孤独
    2020-12-19 05:34

    import csv
    import json
    file1 = csv.DictReader(open('filename.csv', 'r'))
    output =[]
    for each in complent:
        row = {}
        row['Id'] = each['Id']
        row['Name'] = each['Name']
        row['Address'] = each['Address']
        row['Mobile'] = each['Mobile']
        row['LandLine'] = each['LandLine']
        row['Email'] = each['Email']
        output.append(row)
    
    json.dump(output,open('new_file.json','w'),indent=4,sort_keys=False)
    

提交回复
热议问题