Bas*_*asj 3 python email parsing
我想从To:
电子邮件字段解析电子邮件地址。
事实上,当循环mbox中的电子邮件时:
mbox = mailbox.mbox('test.mbox')
for m in mbox:
print m['To']
Run Code Online (Sandbox Code Playgroud)
我们可以得到类似的东西:
info@test.org, Blahblah <blah@test.com>, <another@blah.org>, "Hey" <last@one.com>
Run Code Online (Sandbox Code Playgroud)
这应该被解析为:
[{email: "info@test.org", name: ""},
{email: "blah@test.com", name: "Blahblah"},
{email: "another@blah.org", name: ""},
{email: "last@one.com", name: "Hey"}]
Run Code Online (Sandbox Code Playgroud)
是否已经内置了某些东西(在mailbox
或另一个模块中)为此或什么都没有?
我读了几次这个文档,但没有找到相关的内容。
您可以email.utils.getaddresses()
为此使用:
>>> getaddresses(['info@test.org, Blahblah <blah@test.com>, <another@blah.org>, "Hey" <last@one.com>'])
[('', 'info@test.org'), ('Blahblah', 'blah@test.com'), ('', 'another@blah.org'), ('Hey', 'last@one.com')]
Run Code Online (Sandbox Code Playgroud)
(请注意,该函数需要一个列表,因此您必须将字符串括在 中[...]
。)