On Friday, 29 April 2016 01:19:28 UTC+10, Dan Strohl wrote: > If I am reading this correctly... you have something like (you will have to > excuse my lack of knowledge about what kinds of information these actually > are): > > <race id=1> > <nomination>1234</nomination> > <meeting>first</meeting> > </race> > <race id=2> > <nomination>5678</nomination> > <meeting>second</meeting> > </race> > > > And you want something like: > nominations = [(1,1234), (2,5678)] > meetings = [(1,'first'),(2,'second')] > > if that is correct, my suggestion is to do something like (this is psudeo > code, I didn't look up the exact calls to use): > > nomination_list = [] > meeting_list = [] > > for race_element in xml_file('race'): > id = race_element.get_attr('id') > for nomination_element in race_element('nomination'): > nomination = nomination_element.get_text() > nomination_list.append((id, nomination)) > > for meeting_element in race_element('meeting'): > meeting = meeting_element.get_text() > meeting_list.append((id, meeting)) > > > >
Yes in essence that is what I am trying to acheive however the XML I have has many attributes like this. for example this is one nomination. <nomination number="1" saddlecloth="1" horse="Astern" id="198247" idnumber="" regnumber="" blinkers="0" trainernumber="235" trainersurname="O'Shea" trainerfirstname="John" trainertrack="Agnes Banks/Hawkesbury" rsbtrainername="John O'Shea" jockeynumber="86876" jockeysurname="McDonald" jockeyfirstname="James" barrier="10" weight="56.5" rating="0" description="B C 2 Medaglia D'oro(USA) x Essaouira (Exceed And Excel)" colours="Royal Blue" owners="Godolphin" dob="2013-09-24T00:00:00" age="3" sex="C" career="3-2-0-0 $220750.00" thistrack="1-1-0-0 $68750.00" thisdistance="1-1-0-0 $152000.00" goodtrack="3-2-0-0 $220750.00" heavytrack="0-0-0-0" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="2-2-0-0 $220750.00" secondup="1-0-0-0" mindistancewin="0" maxdistancewin="0" finished="1" weightvariation="0" variedweight="56.5" decimalmargin="0.00" penalty="0" pricestarting="$2.15F" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="" /> Therefore I thought that if I tried to do it like the code you posted it would soon become unweildy. > for race_element in xml_file('race'): > id = race_element.get_attr('id') > for nomination_element in race_element('nomination'): > nomination = nomination_element.get_text() > nomination_list.append((id, nomination)) So I created a list of the attributes of each class meeting race nomination and then parsed that list through the list comprehension. On putting out the code though I realised that whilst each class worked I had no way to relate the race to the meeting, the nomination to the race so if I then loaded the csv or created sql to push it to a db it would loose its relation. So when I say meetattrs = ('id', 'venue', 'date', 'rail', 'weather', 'trackcondition') In my thinking this is a table. Meeting id venue date rail weather trackcondition There is no foreign key relation to race, so in this question I am saying shouldn't I put the meeting_id as a foreign key into the race attributes before parsing race and then I can have a 'id' in meeting related to the new 'race_id' in race. The id of race would then be put in nomnation before parsing and I would do the same? Hoping this is clearer, probably a little close to the problem to express it clearly so I apologise for that. Sayth -- https://mail.python.org/mailman/listinfo/python-list