2013-08-30 125 views
-1

我有一個腳本,可以完美地打印變量(由用戶設置)。Python打印輸出到電子郵件

os.system('clear') 

print "Motion Detection Started" 
print "------------------------" 
print "Pixel Threshold (How much) = " + str(threshold) 
print "Sensitivity (changed Pixels) = " + str(sensitivity) 
print "File Path for Image Save  = " + filepath 
print "---------- Motion Capture File Activity --------------" 

我現在希望通過電子郵件發送此代碼給我自己以確認何時運行。我已在腳本電子郵件中使用email.mimieTextmultipart。但是輸出不再只顯示相關變量的代碼。

body = """ Motion Detection Started \n Pixel Threshold (How much) = " + str(threshold) \n Sensitivity (changed Pixels) = " + str(sensitivity) \n File Path for Image Save  = " + filepath """ 

我敢肯定這是「」」包裝,但不清楚是什麼我應該使用?

+1

可能重複[接收和蟒蛇發送電子郵件(http://stackoverflow.com/questions/348392/receive-and-send-emails-in-python) – Aprillion

+0

看看[ 'str.format()'](http://docs.python.org/2/library/stdtypes.html#str.format)。 – thegrinner

+2

@deathApril問題不在於發送電子郵件,而是爲了讓字符串看起來正確。 – Cruncher

回答

1

應該是:

body = " Motion Detection Started \n Pixel Threshold (How much) = " + str(threshold) + \ 
     "\n Sensitivity (changed Pixels) = " + str(sensitivity) + \ 
     "\n File Path for Image Save  = " + filepath 
2

在Python """引號的意思是把他們之間的一切從字面上。

這裏最簡單的解決辦法是定義一個字符串myString="",然後在每個打印語句中,代替打印,您可以追加到您的字符串中myString=myString+"whatever I want to append\n"

+0

實際上,'myString + ='... \ n''看起來對我的眼睛更好,但最簡單的方法就是用'myString + ='替換'print''\ n'+' – Aprillion

+0

@deathApril喜歡遠離+ =。如果我做myString = myString +「字符串」它真的讓我想起字符串是不可變的,而且我沒有改變它。 – Cruncher

+0

沒關係,如果它提醒你,它不會提醒我,因爲'i = i + 2'或'我[「blah」] = i [「blah」] + 2'並不意味着某些東西是不可變的。我不確定什麼是不變性的提示,它沒有比字面上的字符串賦值's =「bbb」'會有更多的後果.. – Aprillion

1

當你做到以下幾點,你告訴它一切有字符串的一部分(注意代碼如何亮點):

body = """ Motion Detection Started \n Pixel Threshold (How much) = " + str(threshold) \n Sensitivity (changed Pixels) = " + str(sensitivity) \n File Path for Image Save  = " + filepath """ 

您需要實際變量添加到字符串,就像你在打印聲明中連接它們一樣。

body = "Motion Detection Started \n Pixel Threshold (How much) = " + str(threshold) + " \n Sensitivity (changed Pixels) = " + str(sensitivity) + "\n File Path for Image Save  = " + filepath 

你也可以做string formatting

body = "Motion Detection Started\nPixel Threshold (How much) = {}\nSensitivity (changed Pixels) = {}\nFile Path for Image Save = {}".format(threshold, sensitivity, filepath) 
0

如果你想在電子郵件的代碼是有點更具可重用性和強大,Template strings可以幫助你。例如,保存電子郵件文本作爲單獨文件的模板,template.txt

Motion Detection Started 
------------------------------------------------------ 
Pixel Threshold (How much) = $threshold 
Sensitivity (changed Pixels) = $sensitivity 
File Path for Image Save  = $filepath 
---------- Motion Capture File Activity -------------- 

,並在你的代碼,創建與類的1個或多個實例發送電子郵件一起一個類(你可以有超過1個模板):

import string 

class DebugEmail():  
    def __init__(self, templateFileName="default_template.txt"): 
     with open(templateFileName) as f: 
      self.template = string.Template(f.read()) 

    def send(self, data): 
     body = self.template.safe_substitute(data) 
     print(body) # replace by sending email 

debugEmail1 = DebugEmail("template.txt") 

# and test it like this: 

threshold = 1 
sensitivity = 1 
debugEmail1.send(locals()) 

sensitivity = 200 
filepath = "file" 
debugEmail1.send(locals())