Python TypeError:无法在我的邮件正文部分连接“str"和“list"对象 [英] Python TypeError: cannot concatenate 'str' and 'list' objects in my email body part of message
问题描述
我试图在电子邮件正文部分的电子邮件代码中发送一些数据.我正在调用一个返回字符串的函数,我正在调用另一个返回列表的函数.我想将这些包含在邮件的电子邮件正文部分中.我收到错误 TypeError: cannot concatenate 'str' and 'list' objects:
I am trying to send some data in my email code in the body part of the email message. I am calling a function which returns a string and I am calling another function which returns a list. I would like to include these into the email body part of the message. I am getting the error TypeError: cannot concatenate 'str' and 'list' objects:
Traceback (most recent call last):
File "E:/test_runners 2 edit project in progress add more tests/selenium_regression_test_5_1_1/Email/email_selenium_report.py", line 32, in <module>
report.send_report_summary_from_htmltestrunner_selenium_report2()
File "E:\test_runners 2 edit project in progress add more tests\selenium_regression_test_5_1_1\Email\report.py", line 520, in send_report_summary_from_htmltestrunner_selenium_report2
extract_testcases_from_report_htmltestrunner()) + "\n Report location = : \\storage-1\Testing\Selenium_Test_Report_Results\ClearCore_5_1_1\Selenium VM\IE11 \n")
TypeError: cannot concatenate 'str' and 'list' objects
我的电子邮件代码中邮件正文部分所在的行:
The line in my email code where the message body part is:
msg = MIMEText("\n ClearCore 5_1_1 Automated GUI Test_IE11_Selenium_VM \n " + extract_only_header_from_summary_from_report_htmltestrunner() + "\n extract_header_count__from_summary_from_report_htmltestrunner() \n" + list(
extract_testcases_from_report_htmltestrunner()) + "\n Report location = : \\storage-1\Testing\Selenium_Test_Report_Results\ClearCore_5_1_1\Selenium VM\IE11 \n")
我的电子邮件代码是:
from email.mime.text import MIMEText
def send_report_summary_from_htmltestrunner_selenium_report2():
print extract_only_header_from_summary_from_report_htmltestrunner()
print extract_header_count__from_summary_from_report_htmltestrunner()
all_testcases = list(extract_testcases_from_report_htmltestrunner())
# print all_data
pprint.pprint(all_testcases)
msg = MIMEText("\n ClearCore 5_1_1 Automated GUI Test_IE11_Selenium_VM \n " + extract_only_header_from_summary_from_report_htmltestrunner() + "\n extract_header_count__from_summary_from_report_htmltestrunner() \n" + list(
extract_testcases_from_report_htmltestrunner()) + "\n Report location = : \\storage-1\Testing\Selenium_Test_Report_Results\ClearCore_5_1_1\Selenium VM\IE11 \n")
msg['Subject'] = "ClearCore 5_1_1 Automated GUI Test"
msg['to'] = "riaz.ladhani@company.onmicrosoft.com"
msg['From'] = "system@company.com"
s = smtplib.SMTP()
s.connect(host=SMTP_SERVER)
s.sendmail(msg['From'], msg['To'], msg.as_string())
s.close()
返回列表的函数是:
def extract_testcases_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")
for div in soup.select("#result_table tr div.testcase"):
yield div.text.strip().encode('utf-8'), div.find_next("a").text.strip().encode('utf-8')
我正在调用的其他两个返回字符串的函数是:
The other 2 functions which I am calling which returns a string are:
def extract_only_header_from_summary_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")
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)
return results
def extract_header_count__from_summary_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")
table = soup.select_one("#result_table")
#Create list here...
results = []
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
如何将来自这些函数的数据包含在电子邮件的正文部分中?我试图连接它,但没有奏效,因为它说你不能连接字符串和列表.
How can I include the data from these functions into the body part of the email message? I tried to concatenate it but that did not work because it says you cannot concatenate string and a list.
谢谢,里亚兹
推荐答案
msg = MIMEText("\n ClearCore 5_1_1 Automated GUI Test_IE11_Selenium_VM \n " + extract_only_header_from_summary_from_report_htmltestrunner() + "\n extract_header_count__from_summary_from_report_htmltestrunner() \n" + list(
extract_testcases_from_report_htmltestrunner()) + "\n Report location = : \\storage-1\Testing\Selenium_Test_Report_Results\ClearCore_5_1_1\Selenium VM\IE11 \n")
您明确地尝试连接一个列表和字符串,这是不可能的.
You are explicitly trying to concat a list and strings which is impossible.
EDIT ALL 你的函数返回一个列表而不是字符串.
EDIT ALL of your functions return a list instead of strings.
如果您的预期结果是一个字符串,您可以使用 '\n'.join
将列表转换为在每个元素之间带有 '\n'
的字符串列表:
If your expected outcome is a string you can use '\n'.join
to turn the list to a string with '\n'
between every element in the list:
msg = MIMEText("\n ClearCore 5_1_1 Automated GUI Test_IE11_Selenium_VM \n " +
'\n'.join(extract_only_header_from_summary_from_report_htmltestrunner()) +
'\n'.join(extract_header_count__from_summary_from_report_htmltestrunner()) +
'\n'.join(extract_testcases_from_report_htmltestrunner()) +
"\n Report location = : \\storage-1\Testing\Selenium_Test_Report_Results\ClearCore_5_1_1\Selenium VM\IE11 \n")
我可能在格式化时丢失了一些 '\n'
,如果需要,应该重新添加它们.
I might have lost some '\n'
when formatting, you should add them back if you require them.
例如,如果您将函数的返回值更改为字符串,而不是在调用内部转换为MIMEText
,那么它也会更具可读性
It will also be a bit more readable if you change the return values of the function to strings instead of converting inside the call to MIMEText
, for example
def extract_only_header_from_summary_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")
table = soup.select_one("#result_table")
results = []
headers = [td.text for td in table.select_one("#header_row").find_all("td")[1:-1]]
results.append(headers)
# Use whatever character you want as a "separator" instead of '\n'
return '\n'.join(results)
这篇关于Python TypeError:无法在我的邮件正文部分连接“str"和“list"对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!