Created
January 4, 2021 08:26
-
-
Save Mr-Perfection/4351e696abff83b8be9063593aecb650 to your computer and use it in GitHub Desktop.
Convert .JSON file to .csv file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import csv | |
import sys | |
def process(): | |
arguments = sys.argv | |
if len(arguments) != 2: | |
print('please pass your json file as argument i.e. python json_to_csv.py [FILENAME].json') | |
return | |
json_file = arguments[1] | |
filename = json_file.split('.')[0] | |
with open(json_file) as json_file: | |
data = json.load(json_file) | |
# now we will open a file for writing | |
data_file = open(filename+'.csv', 'w') | |
# create the csv writer object | |
csv_writer = csv.writer(data_file) | |
count = 0 | |
keys = data.keys() | |
for key in keys: | |
if count == 0: | |
# Writing headers of CSV file | |
csv_writer.writerow(["email", "name"]) | |
count += 1 | |
# Writing data of CSV file | |
csv_writer.writerow([key, data[key]]) | |
data_file.close() | |
process() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment