python循环附加所有内容
下面的代码将每个迭代追加到下一个迭代。每个文件应该有大约50名玩家,但文件名team1为50,文件名team2为100(team1+team2),依此类推。如何仅使用year+team的1次迭代创建单个文件python循环附加所有内容,python,loops,Python,Loops,下面的代码将每个迭代追加到下一个迭代。每个文件应该有大约50名玩家,但文件名team1为50,文件名team2为100(team1+team2),依此类推。如何仅使用year+team的1次迭代创建单个文件 from nfl_fun import make_soup import os from itertools import islice import csv from datetime import datetime years = [2019,2018,2017,2016,2015]
from nfl_fun import make_soup
import os
from itertools import islice
import csv
from datetime import datetime
years = [2019,2018,2017,2016,2015]
year = datetime.now().year
if year not in years:
years.append(year)
linkname = ""
with open("teamlink.csv") as tl:
for row in islice(csv.reader(tl), 1, None):
for season in years:
rowlink = f"https://www.footballdb.com/{row[0]}/roster/{season}"
soup = make_soup(rowlink)
try:
for boot in soup.findAll('b'):
for link in boot.findAll('a'):
if link.has_attr('href'):
linkname = linkname + "\n" + (link.attrs['href'])[1:]
userfile = f"{rowlink[37:-12]}-{season}"
header="Links"
file = open(os.path.expanduser(f"{userfile}.csv"), "wb")
file.write(bytes(header, encoding="ascii", errors='ignore'))
file.write(bytes(linkname, encoding="ascii",errors='ignore'))
file.close()
except:
continue
每次更改团队时都需要重置
linkname
,您只需添加
linkname = ""
在
file.close()
或类似的内容之后。谢谢,我在几个小时前尝试过,然后它只为每个文件写入一个名称。但在我回答之前,我再次尝试了你的解决方案,它成功了。FML再次感谢您有时您需要另一双眼睛,而且多个嵌套的眼睛很容易混淆