-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathjson2csv.py
52 lines (41 loc) Β· 1.48 KB
/
json2csv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import json
import csv
import os
print("\nWelcome to the JSON to CSV converter")
class Converter:
def converter(self, json_file_path, csv_file_name):
global data
if json_file_path == 1:
try:
with open(json_file_path, 'r') as f1:
data = json.load(f1)
except:
print("File not found")
print("Try creating a new json file")
return
if json_file_path == 2:
f = open("input.json",'x')
f.close()
file = "notepad input.json"
os.system(file)
f1 = open("input.json",'r')
data = json.load(f1)
f2 = open(csv_file_name, 'w', newline="")
writer_object = csv.writer(f2)
writer_object.writerow(data[0].keys())
for row in data:
writer_object.writerow(row.values())
f2.close()
choice = input("Do you want open the converted file? (y/n): ")
while choice not in ['y','n']:
print("Invalid choice")
choice = input("Do you want open the converted file? (y/n): ")
if choice == 'y':
os.system(f'start "excel" {csv_file_name}')
json2csv = Converter()
json_file_path = int(input("""
Press (1) to Open an existing json file
Press (2) to Create a new json file and Enter your data
Choose any one: """))
csv_file_name = input("Name your CSV file: ")+".csv"
json2csv.converter(json_file_path, csv_file_name)