You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
808 B
Python
27 lines
808 B
Python
import csv
|
|
import json
|
|
|
|
def csv_to_json(csvFilePath, jsonFilePath):
|
|
jsonArray = []
|
|
|
|
#read csv file
|
|
with open(csvFilePath, encoding='utf-8') as csvf:
|
|
#load csv file data using csv library's dictionary reader
|
|
csvReader = csv.DictReader(csvf)
|
|
|
|
#convert each csv row into python dict
|
|
for row in csvReader:
|
|
#add this python dict to json array
|
|
jsonArray.append(row)
|
|
|
|
#convert python jsonArray to JSON String and write to file
|
|
with open(jsonFilePath, 'w', encoding='utf-8') as jsonf:
|
|
jsonString = json.dumps(jsonArray, ensure_ascii=False, indent=4)
|
|
print(jsonString)
|
|
jsonf.write(jsonString)
|
|
|
|
csvFilePath = r'playlist.csv'
|
|
jsonFilePath = r'playlist.json'
|
|
|
|
csv_to_json(csvFilePath, jsonFilePath)
|