Python类型错误:序列项0:应为字符串,在电子邮件cod的邮件正文中找到列表

2024-03-29 11:49:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在邮件正文部分的电子邮件代码中发送一些数据。我调用两个函数,返回一个列表。我想把这些包含在邮件正文部分。我得到错误类型错误:序列项0:应为字符串

    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 318, in send_report_summary_from_htmltestrunner_selenium_report2
    '\n'.join(extract_testcases_from_report_htmltestrunner()) +
TypeError: sequence item 0: expected string, list found

我的电子邮件代码是:

^{pr2}$

我的3个返回列表的函数是:

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

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')

如何将函数的返回值包含到电子邮件正文中?在


Tags: infromtestreporthtmlseleniumtablefilename
2条回答

您的函数extract_testcases_from_report_htmltestrunner返回一个元组,而不是一个值。所以基本上,你可能会得到这样的结果:

'\n'.join([('a','b'), ('c', 'd')]

这在python中根本不起作用。在

extract_only_header_from_summary_from_report_htmltestrunner()和{}都生成序列序列(第一个是列表列表,另一个是生成元组的生成器)。这两种方法都不适用于str.join(),它只能接受一系列字符串。在

要么单独连接这些嵌套的序列,要么展平这些序列。在

可以使用嵌套列表理解来连接嵌套序列:

'\n'.join([' - '.join(seq) for seq in extract_only_header_from_summary_from_report_htmltestrunner()])

或者只需嵌套循环以展开嵌套结构:

^{pr2}$

后者也可以通过^{} function实现:

'\n'.join(chain.from_iterable(extract_only_header_from_summary_from_report_htmltestrunner()))

不同之处在于您希望结果是什么;如果[('a', 'b'), ('c', 'd')]都以不同的行结束,则使用嵌套的str.join()循环将'a'和{}连接在一起,然后将结果与'c'和{}的连接结果连接起来,中间加一个新行。在

请注意,extract_only_header_from_summary_from_report_htmltestrunner()似乎只为一个元素创建了一个嵌套列表:

results = []

headers = [td.text for td in table.select_one("#header_row").find_all("td")[1:-1]]
results.append(headers)

return results

这只是一个调用;您可以返回headers并避免首先打开。在

相关问题 更多 >