-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
89 lines (64 loc) · 2.05 KB
/
main.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
89
'''
>>> import csv
>>> with open('eggs.csv', newline='') as csvfile:
... spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
... for row in spamreader:
... print(', '.join(row))
Spam, Spam, Spam, Spam, Spam, Baked Beans
Spam, Lovely Spam, Wonderful Spam
'''
# %%
import os
import csv
from datetime import datetime
def load_file(path: str):
'''Simply load a CSV file and return it'''
output = []
with open(path) as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for row in reader:
output.append(row)
return output
def save_file(path: str, data):
'''Simply save a CSV file'''
with open(path, 'wt', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_MINIMAL)
writer.writerows(data)
def fix_format(data):
'''Add a column on the left and transform the date to something Notion will read'''
header = True
row: list
for row in data:
# Convert the date on non-header rows
if not header:
row[0] = convert_date(row[0])
# Add a rows to the left
if header:
row.insert(0, 'Game')
else:
row.insert(0, '')
if header:
header = False
def convert_date(text):
'''Convert a date in the form "Mon Feb 24 2020" into the form "2020-02-24"'''
twitch_date = datetime.strptime(text, '%a %b %d %Y')
notion_text = twitch_date.strftime('%Y-%m-%d')
return notion_text
def do_file(path, in_dir, out_dir):
data = load_file(f'{in_dir}/{path}')
fix_format(data)
save_file(f'{out_dir}/{path}', data)
def do_whole_dir(in_dir, out_dir):
files = os.listdir(in_dir)
for file in files:
print(f'Converting {file}...')
do_file(file, in_dir, out_dir)
# %%
# set these to you intake folder and output folder
in_dir = 'data_raw'
out_dir = 'data_notion'
print('Reading files to convert into notion format...')
# do_file(path, in_dir, out_dir)
do_whole_dir(in_dir, out_dir)
print('All files are formatted and saved!')
# %%