-
Notifications
You must be signed in to change notification settings - Fork 0
/
save_to_file.py
executable file
·42 lines (35 loc) · 1.38 KB
/
save_to_file.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
from list_zones import list_zones
from flatten_zones import flatten_zones
from json_to_csv import json_to_csv
import json
def save_to_file(headers, file_path = 'zones_data.json'):
result = list_zones(headers)
print(result)
# Specify the file path
# Write JSON data to a file
with open(file_path, 'w', encoding='utf-8') as f:
json.dump(result, f, indent=4) # indent=4 for pretty printing
print(f"Data successfully written to {file_path}")
# Load JSON data from the file
with open(file_path, 'r', encoding='utf-8') as file:
try:
json_data = json.load(file) # Parse JSON data into a Python dictionary
print(json_data) # Print the loaded data
except json.JSONDecodeError as e:
print(f"Error decoding JSON: {e}")
except Exception as e:
print(f"An error occurred: {e}")
# Check result before passing it to json.loads
if result is None:
print("Result is None, unable to process JSON.")
else:
try:
# json_data = json.loads(result)
# Flatten the zones data
flattened_zones = flatten_zones(json_data)
# Convert to CSV
csv_file_path = 'output.csv'
json_to_csv(flattened_zones, csv_file_path)
except json.JSONDecodeError as e:
print(f"Error decoding JSON: {e}")
return result