This repository has been archived by the owner on Nov 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson2csv.py
88 lines (69 loc) · 2.47 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
File: json2csv.py
Author: Chris K.Y. Fung
website: chriskyfung.github.io
Date: 2024-10-04
Description:
This script reads JSON data from a file, converts it to CSV format with a cumulative length limit for each cell, and saves the CSV data to a file.
Usage:
python json2csv.py
Requirements:
- Python 3.x
License:
AGPL-3.0 License. See LICENSE file for details.
"""
import json
import os
from typing import List, Dict
# Constants
JSON_FILE_PATH = 'nodes-id-url.json'
CSV_FILE_PATH = 'nodes-id-url.csv'
LENGTH_LIMIT = 30000
def read_json_file(file_path: str) -> List[Dict]:
"""Read JSON data from a file."""
if not os.path.exists(file_path):
raise FileNotFoundError(f"File not found: {file_path}")
with open(file_path, 'r', encoding='utf-8') as file:
data = json.load(file)
return data
def convert_to_csv(data: List[Dict], length_limit: int = LENGTH_LIMIT) -> str:
"""Convert JSON data to CSV format with specified length limit."""
csv_lines = []
current_line = ""
current_length = 0
for node in data:
node_str = json.dumps(node)
if current_length + len(node_str) > length_limit:
csv_lines.append(current_line.replace(' ', '').replace('"', '""'))
current_line = node_str
current_length = len(node_str)
else:
if current_line:
current_line += ","
current_line += node_str
current_length += len(node_str)
if current_line:
csv_lines.append(current_line.replace(' ', '').replace('"', '""'))
print(f" The dataset, containing {len(data)} items, has been partitioned into {len(csv_lines)} columns.")
return '"' + '","'.join(csv_lines) + '"'
def save_csv_file(file_path: str, csv_data: str) -> None:
"""Save CSV data to a file."""
with open(file_path, 'w', encoding='utf-8') as file:
file.write(csv_data)
def main():
"""Main function to execute the script."""
try:
print("🚀 Starting JSON to CSV conversion...")
# Read JSON data from file
data = read_json_file(JSON_FILE_PATH)
# Convert JSON data to CSV format
csv_data = convert_to_csv(data)
# Save CSV data to file
save_csv_file(CSV_FILE_PATH, csv_data)
print(f"✅ Data successfully saved to {CSV_FILE_PATH}")
except Exception as e:
print(f"❌ An error occurred: {e}")
if __name__ == "__main__":
main()