【问题标题】:A better code to see if python.email.message.Message part is an attachment一个更好的代码来查看 python.email.message.Message 部分是否是附件
【发布时间】:2011-09-01 09:24:44
【问题描述】:

我有一个 python.email.message.Message 的实例 有 3 个部分,但只有一个是附件

假设msg是Message,代码如下:

for part in msg.walk():
    print part.get_param('attachment', 'noattachment', 'Content-Disposition') 

将打印 '' 或 noattachment,因为标题是:

Content-Disposition:附件; 文件名="abc.zip"

有一种更优雅更友好的方式,所以我也可以使用 reduce() 来计算它们吗?

【问题讨论】:

    标签: python email


    【解决方案1】:
    matching_parts = filter(lambda part: \
      part.get_all('Content-Disposition', [None])[0] == 'attachment', msg.walk())
    print len(matching_parts)
    

    【讨论】:

    • 它不起作用您的代码(可能类似)返回 [] 而对于 msg.walk() 中的部分: print part.get_param('attachment', 'noattachment', 'Content-Disposition' ) == '' 假 假 真
    猜你喜欢
    • 2020-11-01
    • 2010-09-20
    • 2020-06-05
    • 1970-01-01
    • 1970-01-01
    • 2019-09-07
    • 1970-01-01
    • 2016-08-13
    • 2015-10-11
    相关资源
    最近更新 更多