我有一个带for循环的函数。它遍历一个列表并将每个值打印到控制台 我想在函数的末尾添加一个return语句,所以当我调用函数时,我可以返回所有的值。 我想我需要将其作为列表返回。
我的功能是:
def extract_header_from_summary_from_report_htmltestrunner():
filename = (r"C:\temp\selenium_report\ClearCore501_Automated_GUI_TestReport.html") html_report_part = open(filename,'r')
soup = BeautifulSoup(html_report_part, "html.parser")
table = soup.select_one("#result_table")
headers = [td.text for td in table.select_one("#header_row").find_all("td")[1:-1]]
print(" ".join(headers))
for row in table.select("tr.passClass"):
print(" ".join([td.text for td in row.find_all("td")[1:-1]]))
如何将返回结束并从for循环中返回每个值?
for循环打印出以下内容:
Count Pass Fail Error
75 75 0 0
谢谢Riaz
答案 0 :(得分:2)
什么阻止你创建一个空列表并附加到它?
def extract_header_from_summary_from_report_htmltestrunner():
filename = (r"C:\temp\selenium_report\ClearCore501_Automated_GUI_TestReport.html") html_report_part = open(filename,'r')
soup = BeautifulSoup(html_report_part, "html.parser")
table = soup.select_one("#result_table")
#Create list here...
results = []
headers = [td.text for td in table.select_one("#header_row").find_all("td")[1:-1]]
print(" ".join(headers))
#Don't forget to append header (if you want)
results.append(headers)
for row in table.select("tr.passClass"):
#Store row string in variable and append before printing
row_str = " ".join([td.text for td in row.find_all("td")[1:-1]]))
results.append(row_str)
print(row_str)
return results
答案 1 :(得分:1)
您可以产生每个字符串:
list(extract_header_from_summary_from_report_htmltestrunner())
然后调用for row in extract_header_from_summary_from_report_htmltestrunner():
# use each row
或迭代生成器函数:
=(SUMIF(A5:A230,K5:K15,C5:C230))