Parsing “From” addresses from email text

前端 未结 8 2132
春和景丽
春和景丽 2021-02-19 03:56

I\'m trying to extract email addresses from plain text transcripts of emails. I\'ve cobbled together a bit of code to find the addresses themselves, but I don\'t know how to mak

8条回答
  •  灰色年华
    2021-02-19 04:36

    Try this out:

    >>> from email.utils import parseaddr
    
    >>> parseaddr('From: vg@m.com')
    ('', 'vg@m.com')
    
    >>> parseaddr('From: Van Gale ')
    ('Van Gale', 'vg@m.com')
    
    >>> parseaddr('    From: Van Gale    ')
    ('Van Gale', 'vg@m.com')
    
    >>> parseaddr('blah abdf    From: Van Gale    and this')
    ('Van Gale', 'vg@m.com')
    

    Unfortunately it only finds the first email in each line because it's expecting header lines, but maybe that's ok?

提交回复
热议问题