python - Creating loops from xml data -
please @ following code:
from xml.dom import minidom xmldoc = minidom.parse("c:\users\...\xml") #this address 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] matchinfo in matchdata: matchinfo = matchdata.getelementsbytagname("matchinfo")[0] attendance = matchinfo.getelementsbytagname("attendance")[0].firstchild.data result = matchinfo.getelementsbytagname("result")[0] print (matchinfo, "attendance: "+ attendance)
so wrote code parse data xml file. keep getting following error:
traceback (most recent call last): file "c:\users\javi\desktop\csvfile.py", line 28, in <module> matchinfo in matchdata: typeerror: iteration on non-sequence
how fix this?
loop on return value of getelementsbytagname
.
replace following line
matchdata = soccerdocument.getelementsbytagname("matchdata")[0]
to
matchdata = soccerdocument.getelementsbytagname("matchdata")
Comments
Post a Comment