Python打印输出到电子邮件

Python打印输出到电子邮件,python,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 Ima

我有一个脚本,可以完美地打印变量(由用户设置)

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.mimieText
多部分
将电子邮件包含在脚本中。但是输出不再显示相对变量,而是显示代码

    body =  """ Motion Detection Started \n Pixel Threshold (How much)   = " + str(threshold) \n    Sensitivity (changed Pixels) = " + str(sensitivity) \n File Path for Image Save     = " + filepath """
我确信它是“包装器”,但不清楚我应该用什么来代替?

在python
中,
引号的意思是从字面上理解它们之间的一切

这里最简单的解决方案是定义一个字符串
myString=”“
,然后在每个打印语句中,您可以使用
myString=myString+“我想附加的任何内容”\n“

将其附加到字符串中,而不是打印:

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

当您执行以下操作时,您正在告诉它字符串的所有部分(注意代码如何突出显示):

实际上,您需要将变量添加到字符串中,就像在print语句中连接变量一样

body =  "Motion Detection Started \n Pixel Threshold (How much)   = " + str(threshold) + " \n    Sensitivity (changed Pixels) = " + str(sensitivity) + "\n File Path for Image Save     = " + filepath
您还可以执行以下操作:


如果您希望电子邮件代码更具可重用性和健壮性,可能会对您有所帮助。例如,将电子邮件文本另存为单独文件中的模板,
template.txt

Motion Detection Started
------------------------------------------------------
Pixel Threshold (How much)   =  $threshold
Sensitivity (changed Pixels) =  $sensitivity
File Path for Image Save     =  $filepath
---------- Motion Capture File Activity --------------
在代码中,创建一个用于发送电子邮件的类以及该类的一个或多个实例(可以有多个模板):


“查看”的可能重复项。@deathApril问题不是发送电子邮件,而是让字符串看起来正确。@Cruncher哦,我明白了,收回我的投票。事实上,
myString+='…\n'
在我看来更好看,但最简单的方法是将
print
替换为
myString+='\n'+
@deathApril喜欢远离+=。如果我做myString=myString+“string”,它确实会提醒我字符串是不可变的,而且我没有对它进行适当的更改。如果它提醒你,它也不会有助于提醒我,因为
I=I+2
I[“blah”]=I[“blah”]+2
并不意味着某些东西是不可变的。。我不确定不变性的提醒有什么好处,它不会比文字字符串赋值产生更多的后果。@deathApril如果字符串被传递到函数中,那么
myString+=“sjiojfaw”
可能会迫使我认为传递的原始字符串已经改变,事实并非如此。我刚刚为本地堆栈上的引用重新分配了一个值。
body = "Motion Detection Started\nPixel Threshold (How much) = {}\nSensitivity (changed Pixels) = {}\nFile Path for Image Save = {}".format(threshold, sensitivity, filepath)
Motion Detection Started
------------------------------------------------------
Pixel Threshold (How much)   =  $threshold
Sensitivity (changed Pixels) =  $sensitivity
File Path for Image Save     =  $filepath
---------- Motion Capture File Activity --------------
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())