[Tutor] Struggling with writing results of loop into txt file
Matt Dempsey
mizzousundevil at gmail.com
Tue Mar 21 17:17:36 CET 2006
I'm using xmltramp to process some U.S. House of Rep. vote data. I can get
it to print what I want but when I try to write it to a text file I had all
sorts of problems. Can someone tell me what I'm doing wrong? I've done quite
a bit of searching for the solution but as a novice to Python I'm at a loss.
Here's the script:
#votes data
#import xmltramp for reading XML files
import xmltramp
#load up the house page with the vote I want
d= xmltramp.load('http://clerk.house.gov/evs/2006/roll002.xml' )
#loads up the vote data off the site
vd= d['vote-data']
#makes a happy nonhyphenated variable out of meta
md= d['vote-metadata']
#counts the number of votes per roll
votes= len(vd)
#Produces the name, party and outcome of every Arizona house member on this
vote
for each in range(votes):
st= str(vd[each].legislator('state'))
vt= str(vd[each].vote)
nm= (vd[each].legislator('unaccented-name'))
pt= str(vd[each].legislator('party'))
f= file('housevote.txt', 'w')
if st == 'AZ':
f.write(nm)
else:
pass
f.close()
Before trying to write it to a file, my loop looked like this:
for each in range(votes):
st= str(vd[each].legislator('state'))
if st == 'AZ':
print vd[each].legislator('unaccented-name'),
vd[each].legislator('party'), vd[each].vote
else:
pass
Any help would be most appreciated. Thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060321/07b35254/attachment.htm
More information about the Tutor
mailing list