Python - 如果字符串是公式的结果,文本不会出现在代码触发的电子邮件中
Python - Text does not appear in code-triggered email sent if string is result of a formula
我正在尝试编写一个从 Gmail 发送电子邮件的程序,其中包含实时股票报价的正文。我正在使用一个模块来获取字符串格式的股票报价(这可行),并且我编写了一个函数来从 gmail 发送电子邮件。 message_send 函数只有在我给它一个简单的字符串时才有效。如果我将 aapl_string 变量传递给它,它就不起作用。请参阅下面的代码:
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_string 作为 message_send 函数的参数时发送电子邮件但包含空白文本吗?
谢谢!
你可以
message_send("The current value is %s" %price_aapl)
这应该会起作用:)
我假设 price_aapl 是一个整数,如果是这样,那么这就是您的全部问题。这是由于无法将整数添加到字符串,因此您可以做的是使用格式字符串。
例如:
aapl_string = "The current price of AAPL is: %d" % price_aapl
%d 是整数 price_aapl 的占位符。
您可以在这里查看 -> http://www.diveintopython.net/native_data_types/formatting_strings.html
有关格式化字符串的更多信息,请参见 python.
我正在尝试编写一个从 Gmail 发送电子邮件的程序,其中包含实时股票报价的正文。我正在使用一个模块来获取字符串格式的股票报价(这可行),并且我编写了一个函数来从 gmail 发送电子邮件。 message_send 函数只有在我给它一个简单的字符串时才有效。如果我将 aapl_string 变量传递给它,它就不起作用。请参阅下面的代码:
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_string 作为 message_send 函数的参数时发送电子邮件但包含空白文本吗?
谢谢!
你可以
message_send("The current value is %s" %price_aapl)
这应该会起作用:)
我假设 price_aapl 是一个整数,如果是这样,那么这就是您的全部问题。这是由于无法将整数添加到字符串,因此您可以做的是使用格式字符串。 例如:
aapl_string = "The current price of AAPL is: %d" % price_aapl
%d 是整数 price_aapl 的占位符。 您可以在这里查看 -> http://www.diveintopython.net/native_data_types/formatting_strings.html 有关格式化字符串的更多信息,请参见 python.