-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdictionary_creation.py
42 lines (31 loc) · 1.51 KB
/
dictionary_creation.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
import csv
from collections import defaultdict
from itertools import combinations
from tqdm import tqdm
import json
### CREAZIONE DIZIONARIO ###
data_dict = defaultdict(lambda: defaultdict(lambda: {"users": [], "user_count": 0}))
def extract_datetime(datetime_str):
return datetime_str[:16] # Primo 16 caratteri (YYYY-MM-DD HH:MM)
with open('clean_df.csv', 'r', encoding='utf-8') as file:
reader = csv.DictReader(file)
for row in tqdm(reader, desc="Elaborando righe", unit="riga"):
# Unisci la data e l'ora arrotondata
datetime = row['date'] + " " + row['rounded_time']
# Estrai solo la parte utile della data e ora
datetime = extract_datetime(datetime)
# Estrai gli altri valori necessari
parcel = ','.join(map(str, eval(row['parcel']))) # Trasforma la lista in stringa
position = eval(row['position']) # Converte la stringa in una lista
user_id = row['address'] # Uso address come nome utente
# Aggiunta utente al dizionario
if not any(user["id"] == user_id for user in data_dict[datetime][parcel]["users"]):
data_dict[datetime][parcel]["users"].append({
"id": user_id,
"position": position
})
data_dict[datetime][parcel]["user_count"] += 1
### CREAZIONE FILE JSON ###
output_file_path = 'data_dict.json'
with open(output_file_path, 'w', encoding='utf-8') as json_file:
json.dump(data_dict, json_file, ensure_ascii=False, indent=4)