Python TypeError:无法连接邮件正文部分中的'str'和'list'对象
问题描述:
我试图在电子邮件正文部分的电子邮件中发送一些数据。我正在调用一个返回一个字符串的函数,我正在调用另一个返回一个列表的函数。我想将这些添加到邮件的邮件正文部分。 我正在错误类型错误:不能连接“海峡”和“名单”的对象:Python TypeError:无法连接邮件正文部分中的'str'和'list'对象
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
在我的电子邮件代码行的邮件正文部分是:
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'] = "[email protected]"
msg['From'] = "[email protected]"
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')
种
另2种功能,这我打电话返回一个字符串是:
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
我怎么能包括这些功能的数据到电子邮件的正文部分? 我试图连接它,但没有奏效,因为它说你不能连接字符串和列表。
感谢,里亚兹
答
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")
你被明确努力Concat的列表和字符串这是不可能的。
编辑所有你的函数返回一个列表而不是字符串。
如果您预期的结果是,你可以使用'\n'.join
到列表中与'\n'
列表变成一个字符串中的每个元素之间的字符串:
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'
格式化时,您应该添加他们回来,如果你需要他们。
如果你改变了函数的返回值转换为字符串,而不是调用MIMEText
内部转换,例如
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)
答
问题的说明也将是更具可读性一点你必须:
In [2]: lst_of_str = ['abc', 'def']
In [3]: s = 'g'
In [4]: s + lst_of_str
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-45caf5564ff0> in <module>()
----> 1 s + lst_of_str
TypeError: cannot concatenate 'str' and 'list' objects
In [5]: s + ''.join(lst_of_str)
Out[5]: 'gabcdef'
问题是,您必须将您的列表显式转换为字符串。请注意,你不希望的结果清单上调用str
:
In [6]: s + str(lst_of_str)
Out[6]: "g['abc', 'def']"
编辑
在你的函数还要注意你有
yield div.text.strip().encode('utf-8'), div.find_next("a").text.strip().encode('utf-8')
它返回的(str, str)
一个元组而不是一个str。如果你想把它们都包含在你的电子邮件正文中,你需要“让它们变平”。例如,
import itertools
s + '\n'.join(itertools.chain(extract_testcases_from_report_htmltestrunner()))
我试过了你的函数extract_only_header_from_summary_report_htmltestrunner。我得到了错误TypeError:序列项0:期望的字符串,找到的列表 –
突出显示错误的行返回'\ n'.join(结果) –
@RiazLadhani哦。我错过了“结果”是列表清单的事实,尽管似乎没有必要这样做。你可以删除'results'变量,然后'return'\ n'.join(headers)'。 – DeepSpace