Examples of working with the data files

In [1]:
!ls data
Basic Timing Data Protocol Overview.pdf  F1 Qualifying.txt
F1 Practice.txt				 F1 Race.txt
In [2]:
!head -n 3 data/F1\ Practice.txt

In [3]:
from lxml import etree
In [6]:
pl=[]

practice_f='data/F1 Practice.txt'

for xml in open(practice_f, 'r'):
    pl.append(etree.fromstring(xml))

pl[100].attrib
Out[6]:
{'timestamp': '10:49:56.085', 'messagecount': '9716', 'identifier': '101'}
In [7]:
pl[100][0].attrib
Out[7]:
{'row': '12', 'value': 'J. BIANCHI', 'colour': 'WHITE', 'column': '3'}

Practice - DataTable for Screen Display

In [9]:
import pandas as pd

#Hacky load and parse of each row in the datafile
pl=[]
for xml in open('data/F1 Practice.txt', 'r'):
    pl.append(etree.fromstring(xml))

#Dataframe for current state timing screen
df_practice_pos=pd.DataFrame(columns=[
    "timestamp", "time",
    "classpos",  "classpos_colour",
    "racingNumber","racingNumber_colour",
    "name","name_colour",
],index=range(50))

#Column mappings
practiceMap={
    '1':'classpos',
    '2':'racingNumber',
    '3':'name',
    '4':'laptime',
    '5':'gap',
    '6':'sector1',
    '7':'sector2',
    '8':'sector3',
    '9':'laps',
    '21':'sector1_best',
    '22':'sector2_best',
    '23':'sector3_best'
}

def parse_practice(p,df_practice_pos):
    if p.attrib['identifier']=='101' and 'sessionstate' not in p[0].attrib:
        if p[0].attrib['column'] not in ['10','21','22','23']:
            colname=practiceMap[p[0].attrib['column']]
            row=int(p[0].attrib['row'])-1
            df_practice_pos.ix[row]['timestamp']=p.attrib['timestamp']
            tt=p.attrib['timestamp'].replace('.',':').split(':')
            df_practice_pos.ix[row]['time'] = datetime.time(int(tt[0]),int(tt[1]),int(tt[2]),int(tt[3])*1000)
            df_practice_pos.ix[row][colname]=p[0].attrib['value']
            df_practice_pos.ix[row][colname+'_colour']=p[0].attrib['colour']
    return df_practice_pos

for p in pl[:2850]:
    df_practice_pos=parse_practice(p,df_practice_pos)
df_practice_pos
/usr/local/lib/python3.4/dist-packages/pandas/io/excel.py:626: UserWarning: Installed openpyxl is not supported at this time. Use >=1.6.1 and <2.0.0.
  .format(openpyxl_compat.start_ver, openpyxl_compat.stop_ver))
Out[9]:
timestamp time classpos classpos_colour racingNumber racingNumber_colour name name_colour
0 11:05:52.876 11:05:52.876000 1 CYAN 20 WHITE K. MAGNUSSEN WHITE
1 11:03:25.727 11:03:25.727000 2 CYAN 25 RED J. VERGNE WHITE
2 11:03:25.737 11:03:25.737000 3 CYAN 4 RED M. CHILTON WHITE
3 11:03:25.747 11:03:25.747000 4 CYAN 27 RED N. HULKENBERG WHITE
4 11:05:31.807 11:05:31.807000 5 CYAN 99 WHITE A. SUTIL WHITE
5 11:06:03.228 11:06:03.228000 6 CYAN 9 WHITE M. ERICSSON WHITE
6 11:03:25.779 11:03:25.779000 7 CYAN 17 RED J. BIANCHI WHITE
7 11:03:25.788 11:03:25.788000 8 CYAN 26 RED D. KVYAT WHITE
8 11:06:15.133 11:06:15.133000 9 CYAN 8 WHITE R. GROSJEAN WHITE
9 11:03:25.807 11:03:25.807000 10 CYAN 77 RED V. BOTTAS WHITE
10 11:03:25.816 11:03:25.816000 11 CYAN 21 RED E. GUTIERREZ WHITE
11 11:06:12.290 11:06:12.290000 12 CYAN 1 WHITE S. VETTEL WHITE
12 11:05:18.258 11:05:18.258000 13 CYAN 10 WHITE K. KOBAYASHI WHITE
13 11:03:25.855 11:03:25.855000 14 CYAN 7 RED K. RAIKKONEN WHITE
14 11:03:25.864 11:03:25.864000 15 CYAN 22 RED J. BUTTON WHITE
15 11:03:25.874 11:03:25.874000 16 CYAN 13 RED P. MALDONADO WHITE
16 11:03:25.881 11:03:25.881000 17 CYAN 11 RED S. PEREZ WHITE
17 11:03:25.906 11:03:25.906000 18 CYAN 6 RED N. ROSBERG WHITE
18 11:03:25.920 11:03:25.920000 19 CYAN 14 RED F. ALONSO WHITE
19 11:03:25.935 11:03:25.935000 20 CYAN 44 RED L. HAMILTON WHITE
20 11:03:25.945 11:03:25.945000 21 CYAN 3 RED D. RICCIARDO WHITE
21 11:00:06.703 11:00:06.703000 22 CYAN 19 RED F. MASSA WHITE
22 10:49:56.290 10:49:56.290000 CYAN WHITE WHITE
23 10:49:56.291 10:49:56.291000 CYAN WHITE WHITE
24 10:49:56.291 10:49:56.291000 CYAN WHITE WHITE
25 10:49:56.291 10:49:56.291000 CYAN WHITE WHITE
26 10:49:56.291 10:49:56.291000 CYAN WHITE WHITE
27 10:49:56.292 10:49:56.292000 CYAN WHITE WHITE
28 10:49:56.292 10:49:56.292000 CYAN WHITE WHITE
29 10:49:56.292 10:49:56.292000 CYAN WHITE WHITE
30 10:49:56.293 10:49:56.293000 CYAN WHITE WHITE
31 10:49:56.293 10:49:56.293000 CYAN WHITE WHITE
32 10:49:56.293 10:49:56.293000 CYAN WHITE WHITE
33 10:49:56.294 10:49:56.294000 CYAN WHITE WHITE
34 10:49:56.294 10:49:56.294000 CYAN WHITE WHITE
35 10:49:56.294 10:49:56.294000 CYAN WHITE WHITE
36 10:49:56.295 10:49:56.295000 CYAN WHITE WHITE
37 10:49:56.295 10:49:56.295000 CYAN WHITE WHITE
38 10:49:56.295 10:49:56.295000 CYAN WHITE WHITE
39 10:49:56.296 10:49:56.296000 CYAN WHITE WHITE
40 10:49:56.296 10:49:56.296000 CYAN WHITE WHITE
41 10:49:56.296 10:49:56.296000 CYAN WHITE WHITE
42 10:49:56.297 10:49:56.297000 CYAN WHITE WHITE
43 10:49:56.297 10:49:56.297000 CYAN WHITE WHITE
44 NaN NaN NaN NaN NaN NaN NaN NaN
45 NaN NaN NaN NaN NaN NaN NaN NaN
46 NaN NaN NaN NaN NaN NaN NaN NaN
47 NaN NaN NaN NaN NaN NaN NaN NaN
48 NaN NaN NaN NaN NaN NaN NaN NaN
49 NaN NaN NaN NaN NaN NaN NaN NaN
In [ ]: