-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_multicolumn_csv.py
73 lines (62 loc) · 3.1 KB
/
create_multicolumn_csv.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
import os
import json
import csv
def map_unique_categories_with_id(directory):
categories = {}
for root, _, files in os.walk(directory):
for file in files:
if file.endswith('.json'):
file_path = os.path.join(root, file)
try:
with open(file_path, 'r', encoding='utf-8') as f:
data = json.load(f)
for tech in data.get("technologies", []):
for category in tech.get("categories", []):
cat_id = category["id"]
cat_name = category["name"]
categories[cat_id] = cat_name
except json.decoder.JSONDecodeError as e:
print(f"Error decoding JSON from file {file_path}: {e}")
# Optionally, raise an exception or continue to the next file
# raise e
continue
sorted_categories = sorted(categories.items(), key=lambda x: x[0])
return sorted_categories
def find_last_status_200_url(urls):
for url, info in reversed(list(urls.items())):
if info.get("status") == 200:
return url
return None
def process_json_files(directory, csv_file_path):
categories_with_id = map_unique_categories_with_id(directory)
fieldnames = ['url'] + [f'{name}_{cid}' for cid, name in categories_with_id]
processed_urls = set()
with open(csv_file_path, 'w', newline='', encoding='utf-8') as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader()
for root, _, files in os.walk(directory):
for file in files:
if file.endswith('.json'):
file_path = os.path.join(root, file)
with open(file_path, 'r', encoding='utf-8') as f:
data = json.load(f)
last_url = find_last_status_200_url(data.get("urls", {}))
if last_url in processed_urls:
continue
processed_urls.add(last_url)
row = {fieldname: '' for fieldname in fieldnames}
row['url'] = last_url
for tech in data.get("technologies", []):
for category in tech.get("categories", []):
cat_id = category["id"]
cat_name = category["name"]
column_name = f'{cat_name}_{cat_id}'
if column_name in row:
row[column_name] += f'{tech["name"]}; '
for key, value in row.items():
if value and value.endswith('; '):
row[key] = value[:-2]
writer.writerow(row)
directory = 'json_output' # Update this path
csv_file_path = 'multicolumn-csv.csv' # Update this path
process_json_files(directory, csv_file_path)