0

请看下面的代码:

from xml.dom import minidom
xmldoc = minidom.parse("C:\Users\...\xml") #This is just the address to the document
soccerfeed = xmldoc.getElementsByTagName("SoccerFeed")[0] 
soccerdocument = soccerfeed.getElementsByTagName("SoccerDocument")[0] 
competition = soccerdocument.getElementsByTagName("Competition")[0] 
country = competition.getElementsByTagName("Country")[0].firstChild.data 
name = competition.getElementsByTagName("Name")[0].firstChild.data  
season = competition.getElementsByTagName("Stat")[1].firstChild.data 
matchday = competition.getElementsByTagName('Stat')[3].firstChild.data   
lst = [country, name, season, "matchday: "+ matchday]    
print lst

#Match Data
MatchData = soccerdocument.getElementsByTagName("MatchData")[0]    
for MatchInfo in MatchData:
    MatchInfo = MatchData.getElementsByTagName("MatchInfo")[0]  
    Attendance = MatchInfo.getElementsByTagName("Attendance")[0].firstChild.data    
    Result = MatchInfo.getElementsByTagName("Result")[0]   
    print (MatchInfo, "Attendance: "+ Attendance)

所以我只是写了这段代码来解析xml文件中的一些数据。我不断收到以下错误:

Traceback (most recent call last):
  File "C:\Users\Javi\Desktop\csvfile.py", line 28, in <module>
    for MatchInfo in MatchData:
TypeError: iteration over non-sequence

我该如何解决?

4

1 回答 1

0

循环返回getElementsByTagName.

替换以下行

MatchData = soccerdocument.getElementsByTagName("MatchData")[0]

MatchData = soccerdocument.getElementsByTagName("MatchData")
于 2013-07-31T17:54:49.903 回答