Skip to content

Instantly share code, notes, and snippets.

@se7enack
Created February 2, 2024 21:09
Show Gist options
  • Save se7enack/22564b4a75bb6ee859eac9e4eec22ad0 to your computer and use it in GitHub Desktop.
Save se7enack/22564b4a75bb6ee859eac9e4eec22ad0 to your computer and use it in GitHub Desktop.
Converts a comma delimited csv file containing a header row into a json file
#!/usr/bin/env python3
import json
import csv
csv_file_path = "/Users/user/Downloads/file.csv"
json_file_path = "/Users/user/Downloads/file.json"
def step1(csv_file_path, json_file_path):
with open(csv_file_path, "r") as csv_file:
reader = csv.reader(csv_file)
headers = next(reader)
data = []
for row in reader:
data.append(dict(zip(headers, row)))
with open(json_file_path, "w") as json_file:
json.dump(data, json_file)
step2()
def step2():
global records
global data
f = open(json_file_path)
data = json.load(f)
f.close()
records = len(data)
# print(data)
# print(records)
step1(csv_file_path, json_file_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment