Python 2.7 Python-如果字符串是公式的结果,则发送的代码触发电子邮件中不会显示文本
我正试图编写一个程序,从Gmail发送一封电子邮件,其中包含实时股票报价的正文。我正在使用一个模块获取字符串格式的股票报价(这很有效),我还编写了一个函数从gmail发送电子邮件。message_send函数只有在我给它一个简单字符串时才起作用。如果我将aapl_字符串变量传递给它,它将不起作用。见下面的代码:Python 2.7 Python-如果字符串是公式的结果,则发送的代码触发电子邮件中不会显示文本,python-2.7,function,smtp,gmail,Python 2.7,Function,Smtp,Gmail,我正试图编写一个程序,从Gmail发送一封电子邮件,其中包含实时股票报价的正文。我正在使用一个模块获取字符串格式的股票报价(这很有效),我还编写了一个函数从gmail发送电子邮件。message_send函数只有在我给它一个简单字符串时才起作用。如果我将aapl_字符串变量传递给它,它将不起作用。见下面的代码: from yahoo_finance import * import smtplib def message_send(messagebody): fromaddr = 'RE
from yahoo_finance import *
import smtplib
def message_send(messagebody):
fromaddr = 'REDACTED'
toaddrs = 'REDACTED'
msg = messagebody
# Credentials (if needed)
username = 'REDACTED'
password = 'REDACTED'
# The actual mail send
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
aapl = Share('AAPL')
aapl.refresh()
price_aapl = aapl.get_price()
aapl_string = "The current price of AAPL is: " + price_aapl
print(aapl_string)
message_send(aapl_string)
你知道为什么电子邮件会发送,但在使用aapl_字符串作为message_send函数的参数时包含空白文本吗
谢谢 你可以这样做
message_send("The current value is %s" %price_aapl)
这应该可以让它工作:)我假设price\u aapl是一个整数,如果是这样的话,那就是你的全部问题。这是因为无法将整数添加到字符串中,所以可以使用格式字符串。 例: %d是整数价格的占位符。 您可以在这里查看->
有关在python中格式化字符串的详细信息。忘记添加它了-它是一个字符串,并且在代码末尾的print语句可以工作哦,好的,那么您只需放置%s而不是%d
aapl_string = "The current price of AAPL is: %d" % price_aapl