我具有以下功能:
def fisap(self):
print("*" * 42)
print("Nrc", "Data".rjust(10), "Intrari".rjust(13), "Iesiri".rjust(12))
print("*" * 42)
for elem in self.ddop.keys():
if elem in self.ddint.keys():
print(str(elem), str(self.ddop[elem]).rjust(10), str(self.ddint[elem]).rjust(13))
else:
print(str(elem), str(self.ddop[elem]).rjust(10), str(0).rjust(13), str(self.ddies[elem]).rjust(12))
print("*" * 42)
print("Soldul final este de : " + str(self.sold))
它返回需要返回的内容,在这里一切都很好,但是我的问题是我如何将所有此返回文本以这种格式存储在变量,字符串或其他任何可以通过smtplib通过电子邮件发送的内容中。如果我尝试使用以下消息的正文发送电子邮件:selfitem.fisap(),它将发送None。 像这样 :
非常感谢您的帮助。
答案 0 :(得分:0)
您需要在打印之前将整个消息存储在字符串中,然后才能使用return
print
与return
def fisap(self):
elements = []
for elem in self.ddop.keys():
if elem in self.ddint.keys():
elements.append(str(elem), str(self.ddop[elem]).rjust(10), str(self.ddint[elem]).rjust(13))
else:
elements.append(str(elem), str(self.ddop[elem]).rjust(10), str(0).rjust(13), str(self.ddies[elem]).rjust(12))
message = "{divider}\n{headers}\n{elements}\n{divider}\n{total}".format(
divider="*" * 42,
headers='\t'.join(["Nrc", "Data".rjust(10), "Intrari".rjust(13), "Iesiri".rjust(12)]),
elements=elements,
total="Soldul final este de : " + str(self.sold)
)
print(message)
return message
def main():
returned_value = fisap() #fisap() is called and its result is stored in the variable returned_value
print(returned_value) #we can then print it again or treat it as we would a string variable
答案 1 :(得分:0)
谢谢! 根据您的解决方案,我编写了以下代码,它可以正常工作:
def trimitefisaemail(self):
a = ("*" * 42) + "\n"
b = "Nrc" + "Data".rjust(10) + "Intrari".rjust(13) + "Iesiri".rjust(12) + "\n"
c = []
for elem in self.ddop.keys():
if elem in self.ddint.keys():
c.append(str(elem) + str(self.ddop[elem]).rjust(10) + str(self.ddint[elem]).rjust(13))
else:
c.append(str(elem) + str(self.ddop[elem]).rjust(10) + str(0).rjust(13) + str(self.ddies[elem]).rjust(12))
cc = "\n".join(c)
d = "\n" + "Soldul final este de : " + str(self.sold)
message = "%s%s%s%s\n%s%s"%(a,b,a,cc,a,d)
sendemailfunction(message)