Python에서 CSV 파일을 JSON으로 변환
2020. 12. 13. 16:46ㆍ서버 프로그래밍
다음 예제는 JSON 객체로 변환하는 것이지만, 조금만 수정하면 배열로 변환 가능하다
import csv
import json
# Function to convert a CSV to JSON
# Takes the file paths as arguments
def make_json(csvFilePath, jsonFilePath):
# create a dictionary
data = {}
# Open a csv reader called DictReader
with open(csvFilePath, encoding='utf-8') as csvf:
csvReader = csv.DictReader(csvf)
# Convert each row into a dictionary
# and add it to data
for rows in csvReader:
# Assuming a column named 'No' to
# be the primary key
key = rows['No']
data[key] = rows
# Open a json writer, and use the json.dumps()
# function to dump data
with open(jsonFilePath, 'w', encoding='utf-8') as jsonf:
jsonf.write(json.dumps(data, indent=4))
www.geeksforgeeks.org/convert-csv-to-json-using-python/