解析电子邮件标题文本抄送字段的方法?
我有一个抄送标头字段的纯文本,如下所示:
[电子邮件受保护],John Smith <[电子邮件受保护]>,"史密斯,简" [电子邮件受保护]> ?
是否有经过实战测试的模块可以正确解析此内容
(如果是在Python中,那就更好了!电子邮件模块只返回原始文本,没有任何分割它的方法,据我所知) (如果它将姓名和地址拆分为字段,也会有好处)
I have the plain text of a Cc header field that looks like so:
[email protected], John Smith <[email protected]>,"Smith, Jane" <[email protected]>
Are there any battle tested modules for parsing this properly?
(bonus if it's in python! the email module just returns the raw text without any methods for splitting it, AFAIK)
(also bonus if it splits name and address into to fields)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有很多函数可用作标准 python 模块,但我认为您正在寻找
email.utils.parseaddr() 或 email.utils.getaddresses()
There are a bunch of function available as a standard python module, but I think you're looking for
email.utils.parseaddr() or email.utils.getaddresses()
我自己没有使用过它,但在我看来你可以使用 csv打包很容易解析数据。
I haven't used it myself, but it looks to me like you could use the csv package quite easily to parse the data.
下面的内容完全没有必要。我在意识到您可以传递
getaddresses()
一个包含单个字符串(包含多个地址)的列表之前就写了它。我还没有机会查看电子邮件标头中地址的规范,但根据您提供的字符串,此代码应该将其拆分为一个列表,并确保忽略引号内的逗号(因此是名称的一部分)。
给出:
我很想知道其他人会如何解决这个问题!
The bellow is completely unnecessary. I wrote it before realising that you could pass
getaddresses()
a list containing a single string containing multiple addresses.I haven't had a chance to look at the specifications for addresses in email headers, but based on the string you provided, this code should do the job splitting it into a list, making sure to ignore commas if they are within quotes (and therefore part of a name).
Gives:
I'd be interested to see how other people would go about this problem!
将多个电子邮件字符串转换为字典(将多个带有名称的电子邮件转换为一个字符串)。
用逗号分割字符串
email_list = emailstring.split(',')
名称是键,电子邮件是值并制作字典。
结果如下:
注意:
如果有相同的姓名和不同的电子邮件 ID,则跳过一条记录。
《老友记》重复了两次。
Convert multiple E-mail string in to dictionary (Multiple E-Mail with name in to one string).
Split string by Comma
email_list = emailstring.split(',')
name is key and email is value and make dictionary.
Result like this:
Note:
If there is same name with different email id then one record is skip.
"Friends" is duplicate 2 time.