我有以下函数和一些print语句。在每个打印语句中,我想返回它的值,以便我可以使用它并将其添加到我的电子邮件代码中,该代码将每个字符串文本发送到电子邮件中。
我尝试将每个字符串连接成一个变量并将其返回到函数的底部。 e.g。
p_text = p_start_time + p_duration + p_status
return p_text
我收到错误:
File "E:/test_runners 2 edit project in progress add more tests/selenium_regression_test_5_1_1/Email/email_selenium_report.py", line 30, in <module>
report.extract_data_from_report_htmltestrunner()
File "E:\test_runners 2 edit project in progress add more tests\selenium_regression_test_5_1_1\Email\report.py", line 400, in extract_data_from_report_htmltestrunner
p_text = p_start_time + p_duration + p_status
TypeError: unsupported operand type(s) for +: 'Tag' and 'Tag'
我的功能实现是:
def extract_data_from_report_htmltestrunner():
filename = (r"E:\test_runners 2 edit project\selenium_regression_test_5_1_1\TestReport\ClearCore501_Automated_GUI_TestReport.html")
html_report_part = open(filename,'r')
soup = BeautifulSoup(html_report_part, "html.parser")
div_heading = soup.find('div', {'class': 'heading'})
p_start_time = div_heading.find('strong', text='Start Time:').parent
p_start_time.find(text=True, recursive=False)
print p_start_time.text
p_duration = div_heading.find('strong', text='Duration:').parent
p_duration.find(text=True, recursive=False)
print p_duration.text
p_status = div_heading.find('strong', text='Status:').parent
p_status.find(text=True, recursive=False)
print p_status.text
#p_text = p_start_time + p_duration + p_status
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]]))
#return p_text
在我的每个print语句中,如何将其作为字符串变量返回? 返回后,我可以将其包含在电子邮件代码的邮件部分中。
即使是在for循环中的这个print语句,我也希望以某种方式将它返回给字符串变量。
print(" ".join([td.text for td in row.find_all("td")[1:-1]]))
谢谢Riaz
答案 0 :(得分:3)
p_text = p_start_time + p_duration + p_status
在此表达式中,所有操作数均为Tag
instances,您无法将其与+
粘合在一起。您可以做的是将它们转换为字符串,然后连接:
p_text = "".join(map(str, [p_start_time, p_duration, p_status]))