-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseIGPResults.py
More file actions
56 lines (43 loc) · 1.65 KB
/
parseIGPResults.py
File metadata and controls
56 lines (43 loc) · 1.65 KB
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
import lxml.html as lh
from lxml import etree
import argparse
# --- Command line arguments
parser = argparse.ArgumentParser(description='parses iGP data')
parser.add_argument('-f', metavar = 'filename', type=argparse.FileType('r'), help='input results html', required=True)
args = parser.parse_args()
colheaders = ['Lap','Laptime','Gap','kph','Pos','Tyre','Fuel']
# --- convert table html element into hash
def lapTableToDict(tab):
outtab = []
for rowi,row in enumerate(tab.cssselect('tr')):
rowdict = {}
if(not('pit' in row.get('class'))):
cells = row.cssselect('td')
if(len(cells)>0):
for ci,cell in enumerate(cells):
key = colheaders[ci]
rowdict[key] = cell.text_content()
mainkey = int(rowdict['Lap'])
outtab.append(rowdict)
return outtab
def parseLapTime(laptime):
timearr = laptime.split(':')
outtime = 0.0
for timeval in timearr:
outtime = outtime * 60 + float(timeval)
return outtime
htmlcontent = args.f.read()
doc = lh.fromstring(htmlcontent)
tabs = doc.cssselect('table.acp')
#print tabs[0].text_content()
# --- parse the data
tabhash = lapTableToDict(tabs[0])
# --- massage the data
print "Lap,\tLaptime,\tTyre,\tFuel"
for lapi,row in enumerate(tabhash):
lap = lapi+1
#print str(lap) + ": " + str(row)
#print str(lap) + ", " + str(row['Laptime']) + ", " + row['Tyre'] + ", " + row['Fuel']
row['Laptime'] = parseLapTime(row['Laptime'])
print str(lap) + ",\t" + ("%0.3f" % (row['Laptime'])) + ", \t" + row['Tyre'] + ", \t" + row['Fuel']
#print str(lap) + ": " + str(row)