简体   繁体   English

忽略Python多行字符串中的变量

[英]Ignoring variable in Python multiline string

I'm forwarding an HTML e-mail using a Python script, which is in a multi line string. 我正在使用多行字符串中的Python脚本转发HTML电子邮件。 It needs to read 4 externally given variables (which work). 它需要读取4个外部给定的变量(有效)。 But there's a mail:to link which uses %20 and %40. 但是有一个mail:to链接使用%20和%40。 I'm using %s to refer to the other objects. 我正在使用%s来引用其他对象。 When I try to execute the script, it gives the following error: 当我尝试执行脚本时,出现以下错误:

TypeError: %o format: a number is required, not str TypeError:%o格式:必须为数字,而不是str

Which I understand, as it expects the %20 and %40 to be a reference to a variable too. 据我了解,因为它希望%20和%40也会引用变量。 I really want to include the mail:to link, but I can't seem to make Python ignore the values. 我确实想包括mail:to链接,但是我似乎无法使Python忽略这些值。

#!/usr/bin/env python

list1=[0,1,2,3,4];

import mimetypes, os, smtplib, sys, 
from email import encoders
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formatdate
from email.utils import make_msgid


me = "xx@yy.nl"
you = "yy@xx.nl"
DESTINATION=sys.argv[1]
SUBJECT=sys.argv[2]
MESSAGE=sys.argv[3]
PASS='password'
SRVSMTP='mysmtpserv:587'
msg = MIMEMultipart('alternative')
msg['Subject'] = SUBJECT
msg['From'] = me
msg['To'] = you
msg['Date'] = formatdate()
msg['Message-ID'] = make_msgid()

sys.argv[3].split(";")
hostnam, trgname, trgstat, trgseve = sys.argv[3].split(";")
print sys.argv[3].split(";")

text = """ plaintext email here
"""
html = """
<html>
<head>
        <title></title>
</head>
<body>
<p style="margin-left: 80px;"><strong>Trigger:</strong> %s trgname <br />
<strong>Status</strong>: %s  trgstat <br />
<strong>Severity:</strong> %s  trgseve </p>

<p style="margin-left: 40px;"><a href="mailto:xx%40yy.nl?bcc=yy%40xx.com&amp;body=insert%20text%2C%0A%0Ahere%20for%20the%20emailA%5BFirmname%5D%0A%5B%28Address%29%20Adressline2%5D">Click here</a> &nbsp;<strong> %s hostnam.</strong><br />
&nbsp;</p>
<a href="mailto:xxxyyy@yxy.com?subject=Unsubscribe%20me">Click here to unsubscribe</a>&nbsp;</span><br />
&nbsp;</p>
</body>
</html>
""" % ('hostnam', 'trgname', 'trgstat', 'trgseve', 'hostnam')

part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
msg.attach(part1)
msg.attach(part2)
smtpserver = smtplib.SMTP(SRVSMTP)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login(me, PASS)
smtpserver.sendmail(me, you, msg.as_string())
smtpserver.quit()

You could use format call instead of the % you are currently using. 您可以使用format调用来代替当前使用的%

In your example, replace all the %s by {} and then replace % ('hostnam', 'trgname', 'trgstat', 'trgseve', 'hostnam') by .format(hostnam, trgname, trgstat, trgseve, hostnam) 在您的示例中,将所有%s替换为{} ,然后将% ('hostnam', 'trgname', 'trgstat', 'trgseve', 'hostnam') .format(hostnam, trgname, trgstat, trgseve, hostnam)

How about template strings ? 模板字符串呢?

from string import Template

print(Template("here be %20 %40 my ${placeholder}string").safe_substitute(placeholder="amazing"))

# here be %20 %40 my amazing string

Available since Python 2.4 自Python 2.4起可用

You can escape the unwanted % characters by doubling them: 您可以通过将多余的%字符加倍来对其进行转义:

<p style="margin-left: 40px;"><a href="mailto:xx%%40yy.nl?bcc=yy%%40xx.com&amp;body=insert%%20text%%2C%%0A%%0Ahere%%20for%%20the%%20emailA%%5BFirmname%%5D%%0A%%5B%%28Address%%29%%20Adressline2%%5D">Click here</a> &nbsp;<strong> %s hostnam.</strong><br />

It's a bit tedious and the other answers are probably better; 这有点乏味,其他答案可能更好。 I'm just mentioning this for completeness. 我只是为了完整性而提到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM