解析XML文件与许多孩子和孙子

时间:2017-05-05 20:02:52

标签: python xml parsing

我是python的新手。我有这个非常大的xml文件,我想从中提取一些数据。这是一段摘录:

<program>
    <id>38e072a7-8fc9-4f9a-8eac-3957905c0002</id>
    <programID>3853</programID>
    <orchestra>New York Philharmonic</orchestra>
    <season>1842-43</season>
    <concertInfo>
        <eventType>Subscription Season</eventType>
        <Location>Manhattan, NY</Location>
        <Venue>Apollo Rooms</Venue>
        <Date>1842-12-07T05:00:00Z</Date>
        <Time>8:00PM</Time>
    </concertInfo>
    <worksInfo>
        <work ID="52446*">
            <composerName>Beethoven,  Ludwig  van</composerName>
            <workTitle>SYMPHONY NO. 5 IN C MINOR, OP.67</workTitle>
            <conductorName>Hill, Ureli Corelli</conductorName>
        </work>
        <work ID="8834*4">
            <composerName>Weber,  Carl  Maria Von</composerName>
            <workTitle>OBERON</workTitle>
            <movement>"Ozean, du Ungeheuer" (Ocean, thou mighty monster), Reiza (Scene and Aria), Act II</movement>
            <conductorName>Timm, Henry C.</conductorName>
            <soloists>
                <soloist>
                    <soloistName>Otto, Antoinette</soloistName>
                    <soloistInstrument>Soprano</soloistInstrument>
                    <soloistRoles>S</soloistRoles>
                </soloist>
            </soloists>
        </work>
        <work ID="3642*">
            <composerName>Hummel,  Johann</composerName>
            <workTitle>QUINTET, PIANO, D MINOR, OP. 74</workTitle>
            <soloists>
                <soloist>
                    <soloistName>Scharfenberg, William</soloistName>
                    <soloistInstrument>Piano</soloistInstrument>
                    <soloistRoles>A</soloistRoles>
                </soloist>
                <soloist>
                    <soloistName>Hill, Ureli Corelli</soloistName>
                    <soloistInstrument>Violin</soloistInstrument>
                    <soloistRoles>A</soloistRoles>
                </soloist>
                <soloist>
                    <soloistName>Derwort, G. H.</soloistName>
                    <soloistInstrument>Viola</soloistInstrument>
                    <soloistRoles>A</soloistRoles>
                </soloist>
                <soloist>
                    <soloistName>Boucher, Alfred</soloistName>
                    <soloistInstrument>Cello</soloistInstrument>
                    <soloistRoles>A</soloistRoles>
                </soloist>
                <soloist>
                    <soloistName>Rosier, F. W.</soloistName>
                    <soloistInstrument>Contrabass</soloistInstrument>
                    <soloistRoles>A</soloistRoles>
                </soloist>
            </soloists>
        </work>
        <work ID="0*">
            <interval>Intermission</interval>
        </work>
        <work ID="8834*3">
            <composerName>Weber,  Carl  Maria Von</composerName>
            <workTitle>OBERON</workTitle>
            <movement>Overture</movement>
            <conductorName>Etienne, Denis G.</conductorName>
        </work>
        <work ID="8835*1">
            <composerName>Rossini,  Gioachino</composerName>
            <workTitle>ARMIDA</workTitle>
            <movement>Duet</movement>
            <conductorName>Timm, Henry C.</conductorName>
            <soloists>
                <soloist>
                    <soloistName>Otto, Antoinette</soloistName>
                    <soloistInstrument>Soprano</soloistInstrument>
                    <soloistRoles>S</soloistRoles>
                </soloist>
                <soloist>
                    <soloistName>Horn, Charles Edward</soloistName>
                    <soloistInstrument>Tenor</soloistInstrument>
                    <soloistRoles>S</soloistRoles>
                </soloist>
            </soloists>
        </work>
        <work ID="8837*6">
            <composerName>Beethoven,  Ludwig  van</composerName>
            <workTitle>FIDELIO, OP. 72</workTitle>
            <movement>"In Des Lebens Fruhlingstagen...O spur ich nicht linde," Florestan (aria)</movement>
            <conductorName>Timm, Henry C.</conductorName>
            <soloists>
                <soloist>
                    <soloistName>Horn, Charles Edward</soloistName>
                    <soloistInstrument>Tenor</soloistInstrument>
                    <soloistRoles>S</soloistRoles>
                </soloist>
            </soloists>
        </work>
        <work ID="8336*4">
            <composerName>Mozart,  Wolfgang  Amadeus</composerName>
            <workTitle>ABDUCTION FROM THE SERAGLIO,THE, K.384</workTitle>
            <movement>"Ach Ich liebte," Konstanze (aria)</movement>
            <conductorName>Timm, Henry C.</conductorName>
            <soloists>
                <soloist>
                    <soloistName>Otto, Antoinette</soloistName>
                    <soloistInstrument>Soprano</soloistInstrument>
                    <soloistRoles>S</soloistRoles>
                </soloist>
            </soloists>
        </work>
        <work ID="5543*">
            <composerName>Kalliwoda,  Johann  W.</composerName>
            <workTitle>OVERTURE NO. 1, D MINOR, OP. 38</workTitle>
            <conductorName>Timm, Henry C.</conductorName>
        </work>
    </worksInfo>
</program>
<program>

我想要做的是提取以下信息:programID,orchestra,season,eventType,work ID,soloistName,solositInstrument,soloistRole

以下是我正在使用的代码:

import csv
import xml.etree.cElementTree as ET
tree = ET.iterparse('complete.xml.txt')
#root = tree.getroot()


for program in root.iter('program'):
  ID = program.findtext('id')
  programID = program.findtext('programID')
  orchestra = program.findtext('orchestra')
  season = program.findtext('season')

  for concert in program.findall('concertInfo'):
    event = concert.findtext('eventType')

    for worksInfo in program.findall('worksInfo'):
        for work in worksInfo.iter('work'): 
            workid = work.get('ID')
            for soloists in work.iter('soloists'):
                for soloist in soloists.iter('soloist'):
                    soloname = soloist.findtext('soloistName')
                    soloinstrument =                                                             `soloist.findtext('soloistInstrument')`  
                    solorole = soloist.findtext('soloistRoles')
                    #print(soloname, soloinstrument, solorole)
            #print(workid)
    #print(event)              
#print(programID , " , " , orchestra , " , " , season)
with open("nyphil.txt","a") as nyphil:
    nyphilwriter = csv.writer(nyphil)
    nyphilwriter.writerow([programID, orchestra, season, event, workid,    `soloname.encode('utf-8'), soloinstrument, solorole])
nyphil.close()

当我运行此代码时,我只获得最后一个soloistName和soloistInstrumet。我想到的结果有点像每个程序的重复观察。所以我有类似的东西:

13918,纽约爱乐乐团,1842-43,订阅季,52446 *,奥托,安托瓦内特,女高音,S

13918,....,3642 *,Scharfenberg,William,Piano,A

13918,....,3642 *,Hill,Ureli Corelli,Violin,A

等等,直到上一个工作ID:

13918,....,8336 * 4,Otto,Antoinette,Soprano,S

我得到的只是最后的工作:

13918,纽约爱乐乐团,1842-43,订阅季,8336 *,奥托,安托瓦内特,女高音,S

在文件中有超过15,000个程序,例如我发布的示例。我想解析所有这些并提取上面提到的信息。我不完全确定如何去做这件事,我已经在互联网上寻找一种方法来做到这一点,但我尝试的一切都不起作用!

1 个答案:

答案 0 :(得分:0)

你的问题是你误解了循环的工作方式。具体来说,这些值只会在您进入循环时发生变化:

for x in range(10):
    pass

print(x) # prints 9

VS

for x in range(10):
    print(x)

这是两件不同的事情。你在做前者。你需要做的是这样的事情:

with open('nyphil.txt', 'w') as f:
    nyphilwriter = csv.writer(f)        
    for program in root.iter('program'):
        id_ = program.findtext('id')
        program_id = program.findtext('programID')
        orchestra = program.findtext('orchestra')
        season = program.findtext('season')
        for concert in program.findall('concertInfo'):
            event = concert.findtext('eventType')
        for info in program.findall('worksInfo'):
            for work in info.iter('work'):
                work_id = work.get('ID')
                for soloists in work.iter('soloists'):
                    for soloist in soloists.iter('soloist'):
                        # Change this line to whatever you want to write out
                        nyphilwriter.writerow([id, program_id, orchestra, season, event, work_id, soloist.findtext('soloistName')])