在for循环中更改循环字符串 [英] Changing a looping string inside for loop

查看:209
本文介绍了在for循环中更改循环字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的

输出:

Arizona Coyotes Vegas Golden Knights

我想循环上述字符串,以便在每次循环(游戏)之后/tr []更改.下一场比赛的球队的基本情况如下:

I want to loop the string mentioned above, so that after each loop (Game) /tr[ ] changes. The strings of next game's teams are as follows:

"//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[3]/td[2]" #Chicago
"//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[4]/td[2]" #Washington

我正在尝试构建一个程序,该程序在运行该程序时会抓取所有游戏并分别在各自的行上分别打印每个游戏:

I am trying to build a program that scrapes all the games and prints each game separately on their respective rows as I run the program:

Game1 away team Game1 home team
Game2 away team Game2 home team
Game3 away team Game3 home team

预期输出:

Arizona Coyotes Vegas Golden Knights
Chicago Blackhawks Washington Capitals
etc....

推荐答案

尝试一下:

team_string = "//*[@id='teams_wrapper']/div[2]/div[3]/div[2]/div/table/tbody/tr[1]/td[2]"
all_team_list = [team_string[:65]+str(i+1)+team_string[66:] for i in range(0,15)] # Change 15 to 107/109
team_text = [driver.find_element_by_xpath(i).text for i in all_team_list]
team_text = zip(*[team_text[i::2] for i in range(2)])
print(*[f'Game {i+1} away team : {awt}, home team : {hmt}' for i, (awt, hmt) in enumerate(team_text)], sep='\n')

输出:

Game 1 away team : Arizona Coyotes, home team : Vegas Golden Knights
Game 2 away team : Chicago Blackhawks, home team : Washington Capitals
Game 3 away team : Dallas Stars, home team : St Louis Blues
Game 4 away team : Boston Bruins, home team : New Jersey Devils
Game 5 away team : Calgary Flames, home team : Vancouver Canucks
Game 6 away team : Montreal Canadiens, home team : New Jersey Devils
Game 7 away team : New York Islanders, home team : Philadelphia Flyers

这篇关于在for循环中更改循环字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆