這裏是例子: 我試圖抓住一系列的XML頁面,然後從它們中提取數據。Python:循環做同樣的事情到一個項目n次,而不是一次n項目
它下載每個單獨的頁面,因爲while循環被設計成可以執行,但tester()函數從它下載的第一個文件打印數據V次,儘管它每次循環後都要下載並清除文件。
這是殺了我我做錯了什麼?
def tester():
with open('raw.txt') as myFile:
test = linecache.getline('raw.txt', 12)
print test
test = ""
myFile.close
def grab_data(Year, rcvote):
link = "XXX/%s/roll%s.xml" % (Year, rc)
site = urllib2.urlopen(link)
localFile = open('raw.txt', 'w')
localFile.write(site.read(100000))
localFile.close()
tester()
while (V !=0):
rc = str(V)
if (len(rc) == 2):
rc = "0%s" % (rc)
elif (len(rc) == 1):
rc = "00%s" % (rc)
else:
rc = rc
grab_data(Year, rc)
V = V - 1
更多難題:如果直接使用'rcvote'爲'grab_data',爲什麼要傳遞'rc'? –
無關你的問題,試試這一行將'V'轉換爲零填充字符串:'rc ='%03d'%V' – Marty