barilog

エンジニアリング見習い雑記

csvファイルからjsonファイルを出力するコード

忘備録:csvファイルからjsonファイルを出力するコード

import csv , json

csvFilePath = "file.csv"
jsonFilePath = "file.json"
arr = []
#read the csv and add the arr to a arrayn

with open (csvFilePath) as csvFile:
    csvReader = csv.DictReader(csvFile)
    print(csvReader)
    for csvRow in csvReader:
        arr.append(csvRow)

print(arr)

# write the data to a json file
with open(jsonFilePath, "w") as jsonFile:
    jsonFile.write(json.dumps(arr, indent = 4))