如何从python中的字典中提取电子邮件作为键值对?

时间:2015-03-28 22:52:14

标签: python python-3.x

从以下字典结构中,如何提取和打印电子邮件地址?例如。我想看'smauel.david@gmail'4,,sdusa@yahoo.com'1等。

dict_items([('10:04:14', 1), ('3', 6), ('Thu', 6), ('19:51:21', 1),
('2008',27),  ('From', 27), ('11:35:08', 1), ('5', 1),
('sntp@hotmail.com', 3), ('Jan', 27), ('15:46:24', 1), ('14:50:18',
1),  ('11:37:30', 1), ('18:10:48', 1), ('17:07:00', 1), ('09:05:31',
1),  ('10:38:42', 1), ('sdusa@yahoo.com', 1),
('samuel.david@gmail.com', 4) ])

3 个答案:

答案 0 :(得分:2)

使用dict comprehensions

d = dict([('10:04:14', 1), ('3', 6), ('Thu', 6), ('19:51:21', 1),('2008',27),  ('From', 27), ('11:35:08', 1), ('5', 1),('sntp@hotmail.com', 3), ('Jan', 27), ('15:46:24', 1), ('14:50:18',1),  ('11:37:30', 1), ('18:10:48', 1), ('17:07:00', 1), ('09:05:31',1),  ('10:38:42', 1), ('sdusa@yahoo.com', 1),('samuel.david@gmail.com', 4) ])

{email:val for email, val in d.items() if '@' in email }

<强>输出:

 {'sdusa@yahoo.com': 1, 'samuel.david@gmail.com': 4, 'sntp@hotmail.com': 3}

答案 1 :(得分:1)

您可以迭代密钥,只抓取包含@的密钥(假设您的所有密钥都是小时或电子邮件地址):

d = dict([('10:04:14', 1), ('3', 6), ('Thu', 6), ('19:51:21', 1), ('2008',27),  ('From', 27), ('11:35:08', 1), ('5', 1), ('sntp@hotmail.com', 3), ('Jan', 27), ('15:46:24', 1), ('14:50:18', 1),  ('11:37:30', 1), ('18:10:48', 1), ('17:07:00', 1), ('09:05:31', 1),  ('10:38:42', 1), ('sdusa@yahoo.com', 1), ('samuel.david@gmail.com', 4) ])

for key in d:
    if "@" in key:
        print(key)

<强>输出

sntp@hotmail.com
samuel.david@gmail.com
sdusa@yahoo.com

答案 2 :(得分:0)

假设:

>>> d=dict([('10:04:14', 1), ('3', 6), ('Thu', 6), ('19:51:21', 1),
... ('2008',27),  ('From', 27), ('11:35:08', 1), ('5', 1),
... ('sntp@hotmail.com', 3), ('Jan', 27), ('15:46:24', 1), ('14:50:18',
... 1),  ('11:37:30', 1), ('18:10:48', 1), ('17:07:00', 1), ('09:05:31',
... 1),  ('10:38:42', 1), ('sdusa@yahoo.com', 1),
... ('samuel.david@gmail.com', 4) ])

如果地址属于您显示的类型,则可以使用键上的filter

>>> list(filter(lambda s: '@' in s, d.keys()))
['sdusa@yahoo.com', 'samuel.david@gmail.com', 'sntp@hotmail.com']

但是,如果您可能有更完整的字符串表示'Comment' <address>类型(或混合)的RFC822电子邮件地址,则可能需要使用email.utils.parseaddr()将注释(或实名)与实际地址:

>>> from email.utils import parseaddr
>>> parseaddr('"Santa" <Santa@np.org>')
('Santa', 'Santa@np.org')

那么:

>>> d=dict([('10:04:14', 1), ('3', 6), ('Thu', 6), ('19:51:21', 1),
... ('2008',27),  ('From', 27), ('11:35:08', 1), ('5', 1),
... ('sntp@hotmail.com', 3), ('Jan', 27), ('15:46:24', 1), ('14:50:18',
... 1),  ('11:37:30', 1), ('18:10:48', 1), ('17:07:00', 1), ('09:05:31',
... 1),  ('10:38:42', 1), ('sdusa@yahoo.com', 1),
... ('"Sammy Davis, Jr." <samuel.david@gmail.com>', 4) ])
>>> 
>>> from email.utils import parseaddr
>>> [parseaddr(s)[1] for s in d.keys() if '@' in parseaddr(s)[1]]
['sntp@hotmail.com', 'samuel.david@gmail.com', 'sdusa@yahoo.com']

注意:parseaddr仅分离有效但完整的RFC822地址的各个部分;它不会验证它是否是真实地址。