forked from paoloruiz/ootp-pt-analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_new_data.py
44 lines (36 loc) · 1.14 KB
/
parse_new_data.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
from bs4 import BeautifulSoup
import csv
import os
new_files = os.listdir('./unparsed_data/')
for filen in new_files:
filename = './unparsed_data/' + filen
if not '.html' in filename:
continue
filetype = ''
if 'Ovr' in filename:
filetype = 'overall/'
elif 'vL' in filename:
filetype = 'vL/'
elif 'T' in filename:
filetype = 'tourney/'
else:
filetype = 'vR/'
f = open(filename, 'r')
html = f.read()
f.close()
soup = BeautifulSoup(html.replace('\n', ''), "lxml")
table = soup.select_one("table.data.sortable")
headers = [th.text for th in table.select("tr th")]
if headers[0].strip() == '':
headers = headers[1:]
rows = [[td.text for td in row.find_all("td")] for row in table.select("tr + tr")]
os.remove(filename)
for i in range(len(rows)):
if rows[i][0].strip() == '':
rows[i] = rows[i][1:]
rows = list(filter(lambda x: x[0].strip() != '', rows))
o = open('./data/' + filetype + filen.split('.')[0] + '.csv', 'w', newline='')
wr = csv.writer(o)
wr.writerow(headers)
wr.writerows(rows)
o.close()