0

我正在使用Python進行文本處理,其中我正在控制檯日誌中查找特定文本並打印每條匹配的行。這是通過調用函數來完成:收集Python輸出直到當前執行並將其分配給發送電子郵件的變量

get_matched_log_lines(url, search_pattern, print_pattern)其中url =從中我得到我的日誌,search_pattern =我的目標搜索模式,print_pattern =我想打印我的輸出(它,%s.%s

怎樣的方式我是否通過電子郵件發送功能get_matched_log_lines()的整個輸出?電子郵件功能代碼已經由我在Python中編寫。

這是我覺得/迄今爲止嘗試:

email_content = get_matched_log_lines(url, search_pattern, print_pattern) 
TO = 'recipient email address' 
FROM ='sender email address' 

#emailing function - py_mail 
py_mail("Test email subject", email_content, TO, FROM) 

這爲我提供了一個空的電子郵件。

+0

而你的問題是什麼? – PyNEwbie

+0

我剛剛編輯我的問題問...如何通過電子郵件發送函數get_matched_log_lines()的整個輸出?我希望現在更清楚。 –

+0

[如何用Python發送電子郵件?]可能的副本(http://stackoverflow.com/questions/6270782/how-to-send-an-email-with-python) – PyNEwbie

回答

0

下面是PyNEwbie基礎上,建議我的答案:

def get_matched_log_lines(url, search_pattern, print_pattern): 
     out = open("output_file.txt", "w") 
     for something in somthings: 
       test = print_pattern % matched_line 
       print >>out, test 
      out.close() 

^^只是一個普通的例子(語法不正確可能)。這個想法是打開file in write mode,然後在其中傾倒輸出。

fp = open("output_file.txt", 'r') 
    # Create a text/plain message 
    msg = fp.read() 
    fp.close() 
    email_content = msg 

然後打開相同file in read mode,其輸出的一些變種存儲(在我的情況email_content

最後與email_content發送電子郵件,

email_content = get_matched_log_lines(url, search_pattern, print_pattern) 
    TO = 'recipient email address' 
    FROM ='sender email address' 

#emailing function - py_mail 
py_mail("Test email subject", email_content, TO, FROM) 
相關問題