csv to json with python, json in rows

前端 未结 2 1841
自闭症患者
自闭症患者 2021-01-15 01:11

I would like to covert a CSV to a set of JSON objects with Python, formatted in rows.

I tried this script below, put together from a couple SO answers, but this form

相关标签:
2条回答
  • 2021-01-15 01:17

    First, the CSV file is not formatted properly, I had to reformat file.csv to look like:

    SUM_F,SUM_I,SUM_P,SUM_PI,SUM_Bt,SUM_BI,SUM_M,SUM_MI,Year,Month
    15,3963,14,993,0,91,1,2879,2009,1
    3,4,5,6,0,971,1,8,9,10
    

    in order to make it work - I'm not sure if that's the uneven number of spaces between the tokens or other reason.

    Second, I modified the code:

    import sys, getopt
    import csv
    import json
    
    
    CSV_PATH = '/path/file.csv'
    JSON_PATH = '/path/demo.json'
    
    csv_file = csv.DictReader(open(CSV_PATH, 'r'))
    
    f = file(JSON_PATH, 'w')
    for row in csv_file:
        f.write(str(row)+"\n")
    

    and the result (file) looks like:

    {'SUM_I': '3963', 'SUM_M': '1', 'SUM_BI': '91', 'Month': '1', 'SUM_MI': '2879', 'SUM_F': '15', 'Year': '2009', 'SUM_Bt': '0', 'SUM_P': '14', 'SUM_PI': '993'}
    {'SUM_I': '4', 'SUM_M': '1', 'SUM_BI': '971', 'Month': '10', 'SUM_MI': '8', 'SUM_F': '3', 'Year': '9', 'SUM_Bt': '0', 'SUM_P': '5', 'SUM_PI': '6'}
    
    0 讨论(0)
  • 2021-01-15 01:22
    import csv
    import json
    
    CSV_PATH = 'file.csv'
    JSON_PATH = 'demo.json'
    
    with open(CSV_PATH, 'r') as csv_file:
        reader = csv.DictReader(csv_file)
        with open(JSON_PATH, 'w') as json_file:
            for row in reader:
                json_file.write(json.dumps(row) + '\n')
    

    str(row) gives the wrong kind of quotes, don't use it. You won't be able to read the file with json.

    0 讨论(0)
提交回复
热议问题