我正在尝试使用Python向自己发送短信。这是我的代码:
from twilio.rest import TwilioRestClient
account_sid = ""
auth_token = ""
client = TwilioRestClient(account_sid, auth_token)
message = client.messages.create(to="", from_="",
body=
"Some text here...\n"
"\n"
"Stats for this run through:"
"\n"
email_1
email_2
email_3
email_4
email_5
email_6
email_7
email_8
email_9
email_10
email_11
email_12
"\n"
"\n"
email_13
email_14
email_15
)
在上面的示例中,email_1-email_15是我在别处定义的变量。当我尝试运行此代码时,我收到以下错误:
email_1
^
SyntaxError: invalid syntax
我正在尝试获取多行消息,而email_1-15则由一些文本和一个动态变量组成,这些变量从一个元组连接成一个字符串。
示例输出应为:
Some text here...
Stats for this run through:
text_1: 1
text_2: 1
text_3: 1
text_4: 1
text_5: 1
text_6: 1
text_7: 1
text_8: 1
text_9: 1
text_10: 1
text_11: 1
text_12: 1
text_13: 1
text_14: 1
text_15: 1
有谁可以看到这里的问题是什么?
由于
答案 0 :(得分:3)
Python中的无效语法是在代码中插入变量名称。这样的事情应该有效:
message = client.messages.create(to="", from_="",
body=
"Some text here...\n"
"\n"
"Stats for this run through:"
"\n" +
"\n".join([email_1, email_2, email_3, email_4, email_5, email_6, email_7,
email_8, email_9, email_10, email_11, email_12, email_13, email_14,
email_15])
)
我非常强烈建议您定义一个包含电子邮件变量的列表,因为它会使您的代码更简单:
message = client.messages.create(to="", from_="",
body=
"Some text here...\n"
"\n"
"Stats for this run through:"
"\n" +
"\n".join(emails)
)