Hi all can help to enhanced my python code for creating JSON files. I want to create loop that creates json (different json for each of the folder) from the csv files save in different folder and and then save all the json in the one common folder. Right now I am using below code and changing the path last two digits manually to create json one by one that is very tedious task.
Or is possible to create same task in R?
import csv
import json
import glob
import os
class csv2jsonindirectory():
def Python_trial(self):
# Update the following variable with the path in windows and replace
# every "\" with "/".
path_to_folder = "C:\\Users\\CSVs\\AO"
csv_files_in_folder = path_to_folder + '/*.csv'
csvfilenames = []
i = 1
mydict = {}
for filename in glob.glob(csv_files_in_folder):
csvfilenames.append(os.path.splitext(filename)[0])
rows = []
for i in range(len(csvfilenames)):
with open(csvfilenames[i] + ".csv", "r") as f:
csvreader = csv.DictReader(f)
rows = list(csvreader)
mydict["chartdiv" + str(i + 1)] = rows
print(mydict)
with open(csvfilenames[0] + ".json", 'w') as f:
json.dump(mydict, f, indent= 4)
dd = csv2jsonindirectory()
dd.Python_trial()