import urllib
import re
import csv
player_code = open("Desktop/OHL PYTHON/test2.txt").read()
player_code = player_code.split("\r")
for pc in player_code:
htmlfile = urllib.urlopen( "http://www.eliteprospects.com/iframe_player_stats_small.php?player="+pc+"")
htmltext = htmlfile.read()
regex = '<font color="#000099">(.+?)</font>'
pattern = re.compile(regex)
team = re.findall(pattern,htmltext)
data = pc, team
with open('my_games.csv', 'w') as csvfile:
fieldnames = ['pc', 'team','League', 'Gp', 'G','A','P','Pims']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames,delimiter= ":",
extrasaction ='ignore')
i=0
writer.writeheader()
for pc in player_code:
writer.writerow({'pc':[pc],'team':[team]})
i+=1
这只是一遍又一遍地返回一行数据。任何方向都会有所帮助!谢谢。
答案 0 :(得分:0)
您应该在for循环之前打开文件或使用a+
模式。 w
打开文件并在每次打开时截断其数据。
with open('my_games.csv', 'w') as csvfile:
for pc in player_code:
或
with open('my_games.csv', 'a+') as csvfile
打开文件一次是一种更好的方法。
由于您为了写入数据而循环两次,因此您有多行。只需删除
i=0
for pc in player_code:
writer.writerow({'pc':[pc],'team':[team]})
i+=1
而只是拥有,我认为它会起作用。
writer.writerow({'pc':[pc],'team':[team]})