gpt4 book ai didi

python - GAE Python - 如何将 csv.writer 的结果附加到电子邮件中?

转载 作者:太空宇宙 更新时间:2023-11-04 10:48:51 24 4
gpt4 key购买 nike

在 GAE Python 上,我正在从我的数据存储中编写一个 CSV,并希望通过电子邮件发送它而不将其存储在 blobstore 中。这是为了每天作为 CRON 作业运行一次。

导出工作正常,但我不知道如何将 CSV 添加到电子邮件的附件中。非常感谢您的意见。

class ShopExport(webapp2.RequestHandler):
def get(self):
shops = Shop.all().filter('active =', True).order('name')
self.response.headers[str('Content-Type')] = str('application/csv')
self.response.headers[str('Content-Disposition')] = str('attachment; filename="shops.csv"')
writer = unicodecsv.writer(self.response.out, encoding='utf-8')
writer.writerow(["id", "name", "domain", "category", "deeplink", "region"])
writer.writerow([shop.keyname, shop.name, shop.url, shop.category, shop.url_aff, region])

message = mail.EmailMessage(sender="Me <me@gmail.com>",
subject="Shop Export",
attachments=[("shops.csv", self.response.out)])
message.to="Someone <someone@gmail.com>",
message.html= 'Please find attached the shop export'
message.send()

最佳答案

你可以这样做

import StringIO
...
data = StringIO.StringIO()
writer = unicodecsv.writer(data, encoding='utf-8')
...
message = mail.EmailMessage(sender="Me <me@gmail.com>",
subject="Shop Export",
attachments=[("shops.csv", bytes(data.getvalue()))])

关于python - GAE Python - 如何将 csv.writer 的结果附加到电子邮件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15449371/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com