简体   繁体   中英

Parsing XML using LXML and Python

I have read the other questions on stack overflow but am still unsure how to proceed. I have simplified the XML document for ease of reading.

         <event>
            <time>2015-01-30T08:59:00Z</time>
            <homeTeam type="Team1">
              <name>United Arab Emirates</name>
            </homeTeam>
            <awayTeam type="Team2">
              <name>Iraq</name>
            </awayTeam>
            <periods>
              <period lineId="168809488">
                <number>0</number>
                <description>Match</description>
                <cutoffDateTime>2015-01-30T08:59:00Z</cutoffDateTime>
                <moneyLine>
                  <awayPrice>218</awayPrice>
                  <homePrice>148</homePrice>
                  <drawPrice>225</drawPrice>
                </moneyLine>
              </period>
            </periods>
          </event>

There are several 'event' elements in the XML file. I can isolate each one using the tree.getiterator('event')

for elt in tree.getiterator('event'):
    print elt.xpath('./homeTeam/name/text()')
    print elt.xpath('//startDateTime/text()') 

However, this produces two lists. I need to isolate each time, Home Team Name, Away Team Name, Away Price, Home Price and Draw Price in order to store into a Mysql DB.

I am not sure how to do this iterating through the event without ending up with different lists. One containing all Times, One containing all Home Team Names etc

Any advice or pointers would be appreciated

You will end up with different lists as you are looking for different things, but that should be alright. The goal here is not to immediately get the result out of an lxml interface, but get something later that you can then put into a mysql database, yes?

If so, simply adding a call to zip to your insert process should be just fine. For example:

home_teams = elt.xpath('./homeTeam/name/text()')
away_teams = # something
away_prices = # something

full_iterator = zip(home_teams, away_teams, away_prices, etc...)
for values in full_iterator:
    # For example
    mysql.insert(*values)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM