-
Notifications
You must be signed in to change notification settings - Fork 2
/
add_history.py
31 lines (28 loc) · 1.01 KB
/
add_history.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
import json
from constants import (
CURRENT_GAS_HISTORY_JSON_FILE,
CURRENT_WEEK,
START_DATE_KEY,
CURRENT_GAS_HISTORY_CSV_FILE,
END_DATE_KEY,
COLORED_DIESEL,
GASOLINE_98,
GASOLINE_95,
DIESEL,
GAS_KEY,
)
def add_history(dict_prices):
# JSON
with open(CURRENT_GAS_HISTORY_JSON_FILE, "r") as f:
current_data = json.load(f)
with open(CURRENT_GAS_HISTORY_JSON_FILE, "w") as f:
current_data[dict_prices[CURRENT_WEEK][START_DATE_KEY]] = dict_prices[
CURRENT_WEEK
]
content = json.dumps(current_data, indent=1, ensure_ascii=False)
f.write(content)
# CSV
with open(CURRENT_GAS_HISTORY_CSV_FILE, "a") as f:
f.write(
f"{dict_prices[CURRENT_WEEK][START_DATE_KEY]},{dict_prices[CURRENT_WEEK][END_DATE_KEY]},{dict_prices[CURRENT_WEEK][GAS_KEY][GASOLINE_95]},{dict_prices[CURRENT_WEEK][GAS_KEY][DIESEL]},{dict_prices[CURRENT_WEEK][GAS_KEY][COLORED_DIESEL]},{dict_prices[CURRENT_WEEK][GAS_KEY][GASOLINE_98]}\n"
)