-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput.py
55 lines (47 loc) · 1.56 KB
/
output.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
import csv
import openpyxl
import pathlib
def write_csv(horse_lst, owner_id, header, path):
"""
Input:
horse_lst - list
owner_id - integer
header - list
path - pathlib object
"""
file_path = path.joinpath((str(owner_id) + '.csv'))
with open(file_path, 'w', encoding='utf-8', newline='') as f:
writer = csv.writer(f)
writer.writerow(header)
for i in horse_lst:
for j in i['races']:
writer.writerow([i['horse_name'],
i['horse_url'],
j['race_url'],
j['race_date'],
j['owner'],
j['prize'],
j['currency']])
def write_xlsx(horse_lst, owner_id, header, path):
"""
Input:
horse_lst - list
owner_id - integer
header - list
path - pathlib object
"""
file_path = path.joinpath((str(owner_id) + '.xlsx'))
workbook = openpyxl.Workbook()
worksheet = workbook.active
worksheet.title = 'Owner ID ' + str(owner_id)
worksheet.append(item for item in header)
for i in horse_lst:
for j in i['races']:
worksheet.append([i['horse_name'],
i['horse_url'],
j['race_url'],
j['race_date'],
j['owner'],
j['prize'],
j['currency']])
workbook.save(filename=file_path)