2016-08-15 2 views
0

이메일 메시지의 본문 부분에있는 이메일 코드에 일부 데이터를 보내려고합니다. 문자열을 반환하는 함수를 호출하고 목록을 반환하는 다른 함수를 호출하고 있습니다. 메시지 본문 부분에이를 포함시키고 싶습니다. 내가 오류 형식 오류를 얻고있다 :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") 

내 이메일 코드는 'STR'과 '목록'개체에 연결할 수 없습니다 :

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[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') 
내가 문자열을 반환하는 호출 오전 617,451,515,

다른이 개 기능은 다음과 같습니다

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 
내가 전자 메일 메시지의 본문 부분으로 이러한 기능의 데이터를 포함 할 수 있습니다 어떻게

? 문자열을 연결하려고했으나 문자열과 목록을 연결할 수 없다는 메시지가 표시되어 작동하지 않았습니다.

감사합니다, 리아 즈

답변

1
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'와 문자열로 목록을 설정하는 '\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") 

은 그 (것)들을 요구하는 경우에 후에 그 (것)들.

당신이 예를

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) 
+0

나는 extract_only_header_from_summary_report_htmltestrunner 함수를 사용해 보았습니다. 오류가 발생했습니다. TypeError : 시퀀스 항목 0 : 예상 문자열, 목록을 찾았습니다. –

+0

오류로 강조 표시된 행은 '\ n'에 있습니다. 조인 (결과) –

+0

@RiazLadhani Oh. 나는 결과가 목록의 목록이라는 사실을 놓쳤다. 그러나 그것이 필요하지는 않은 것으로 보인다. 'results' 변수를 제거하고 \ n'.join (headers)'를 리턴 할 수 있습니다. – DeepSpace

-2

를 들어, 대신 MIMEText에 대한 호출 내부 변환 문자열로 문제의 그림을 함수의 반환 값을 변경하는 경우 또한 좀 더 읽을 수, 당신은 :

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']" 

편집

또한 당신이 (str, str)의 튜플을 반환

yield div.text.strip().encode('utf-8'), div.find_next("a").text.strip().encode('utf-8') 

이 함수에주의 str 대신에. 이메일 본문에이 두 가지를 모두 포함하려면 "평평하게"만들 필요가 있습니다. 예 :

import itertools 
s + '\n'.join(itertools.chain(extract_testcases_from_report_htmltestrunner())) 
+0

나는 OP가''[ 'abc', 'def'] "'를 출력으로하는 것을 의심합니다. '', '.join (['abc ','def '])'''abc, def''에 대해'join'을 사용하는 것이 더 좋습니다. – DeepSpace

+0

@DeepSpace'str (list)'에 의해 나는 그렇게하지 않기를 바랬다. – ShuaiYuan

관련 문제