Python中的字符串操作
我试图写一个命令,但我不希望一个长的行,看起来不整洁。我希望将字符串添加到一起,以便按命令执行。我在下面有一些代码,这是电子邮件功能的一部分:Python中的字符串操作,python,Python,我试图写一个命令,但我不希望一个长的行,看起来不整洁。我希望将字符串添加到一起,以便按命令执行。我在下面有一些代码,这是电子邮件功能的一部分: msg = MIMEText("The nightly build status was a SUCCESS\n\nBuild File: http://www.python.org\n\n Build Results File: http://10.51.54.57/sandboxes/", project, "\n") 这显示了一条线,我希望有更好
msg = MIMEText("The nightly build status was a SUCCESS\n\nBuild File: http://www.python.org\n\n Build Results File: http://10.51.54.57/sandboxes/", project, "\n")
这显示了一条线,我希望有更好的方法来做到这一点。我已经尝试了下面的代码,但它不工作
msg = MIMEText("The nightly build status was a SUCCESS\n\nBuild File: ")
msg += MIMEText("http://www.python.org\n\n Build Results File: ")
msg += MIMEText("http://10.51.54.57/sandboxes/", project, "\n")
谢谢你的帮助
我尝试了以下代码,但得到:
msg = MIMEText("""The nightly build status was a SUCCESS\n\n
Build File: """,
build_file, """
\n\n
Build Results File: """,
build_file, """
\n\n
Sandbox Folder:""",
sandbox, """
\n\n
Antibrick File: """,
antibrick, "\n\n")
现在我明白了:
Traceback (most recent call last):
File "test_email.py", line 45, in <module>
if __name__ == '__main__': myObject = email_success()
File "test_email.py", line 32, in email_success
antibrick, "\n\n")
TypeError: __init__() takes at most 4 arguments (10 given)
尝试:
如果每行开头的额外空格有问题,请使用regexp(
r'^\s+'
)删除它们。您可以使用三重引号
>>> s="""The nightly build status was a SUCCESS
Build File: http://www.python.org
Build Results File: http://10.51.54.57/sandboxes/"""
>>> msg=MimeType(s,project,"\n")
怎么样
msg = MIMEText(
"The nightly build status was a SUCCESS\n\n"
"Build File: http://www.python.org\n\n"
"Build Results File: http://10.51.54.57/sandboxes/"
, project
, "\n"
)
或
或
更新:因为OP添加了另一个问题
msg=MIMEText("""The nightly build status was a SUCCESS\n\n
Build File: %s
\n\n
Build Results File: %s
\n\n
Sandbox Folder: %s
\n\n
Antibrick File: """ % (build_file,build_file,sandbox),
antibrick,
"\n\n"
)
Python支持使用三重引号的多行字符串:
text = """The nightly build status was a SUCCESS\n\nBuild File:
http://www.python.org\n\n Build Results File:
http://10.51.54.57/sandboxes/"""
msg = MIMEText(text, project, "\n")
为什么不呢
msg= MIMEText("The nightly build status was a SUCCESS\n\nBuild File: "+ \
"http://www.python.org\n\n Bu..... ")
等等
(即,使用直线延续反斜杠)
还要注意的是,以下每一项都给出了abcdef
而且
s="""xyz
wvu"""
给你
'xyz\nwvu'
嗯,你使用的模块是什么?我猜,这是不推荐的,因为现代界面是(如果我猜对了你的意图的话)。更具体地说,创建您使用的
MIMEText
对象。签名是
email.mime.text.MIMEText(_text[, _subtype[, _charset]])
请指定您遇到的错误。可能无法像那样将
MIMEText
连接在一起。错误说明了一切!!您有10个参数传递给MIMEText。正确构建您的消息。
msg= MIMEText("The nightly build status was a SUCCESS\n\nBuild File: "+ \
"http://www.python.org\n\n Bu..... ")
s ="abc" "def"
s= "abc" \
"def"
s="""xyz
wvu"""
'xyz\nwvu'
email.mime.text.MIMEText(_text[, _subtype[, _charset]])