简体   繁体   English

从电子邮件中提取并计算域地址邮件

[英]Extract and count domains address mails from e-mails

I have a list of emails and would like to extract only the domains and count how many times each one appears: 我有一封电子邮件列表,只想提取域并计算每个域出现多少次:

Emails: 电邮:

best@yahoo.com best@yahoo.com

hello@gmail.com hello@gmail.com

everybody@gmail.com 每个人@ gmail.com

bye@gmail.com bye@gmail.com

day@yahoo.com day@yahoo.com

table.blue@gmail.com table.blue@gmail.com

life@yahoo.com life@yahoo.com

Script: 脚本:

import re
from collections import Counter

with open("mails.txt", "r") as f:
    texte = f.read().split('\n')

    for line in texte:
        newline = re.search("@[\w.]+", line)
        newmail = newline.group()

        mails_value = Counter(newmail).most_common()

        print (mails_value)

output: 输出:

[('@', 1), ('g', 1), ('6', 1), ('5', 1), ('.', 1), ('f', 1), ('r', 1)] [('@',1),('g',1),('6',1),('5',1),('。',1),('f',1),( 'r',1)]

Traceback (most recent call last): 追溯(最近一次通话):

File "counting.py", line 10, in 文件“ counting.py”,第10行,在

 newmail = newline.group() 

AttributeError: 'NoneType' object has no attribute 'group' AttributeError:'NoneType'对象没有属性'group'

good output: 好的输出:

@yahoo.com 3 @ yahoo.com 3

@gmail.com 4 @ gmail.com 4

You're pretty close - No need to split the file into lines, just use re.findall , re.MULTILINE and the pattern @(.*)$ 您非常接近-无需将文件拆分为行,只需使用re.findallre.MULTILINE和模式@(。*)$

import re
import collections

with open("mails.txt") as f:
    text = f.read()
domains = re.findall(r'@(.*)$', text, re.MULTILINE)
mails_value = collections.Counter(domains) 
# outputs with example: Counter({'gmail.com': 4, 'yahoo.com': 3})

You don't need a regex. 您不需要正则表达式。 If you can trust that all the inputs are well formed emails, this should suffice: 如果您可以确信所有输入内容都是格式正确的电子邮件,那么就足够了:

from collections import defaultdict

domain_count = defaultdict(lambda: 0)

with open("mails.txt", "r") as f:
    texte = f.readlines()

    for line in texte:
        domain = line.split('@')[-1]
        domain_count[domain] += 1

print (domain_count)

The regex will save you from creating an unnecessary list. 正则表达式可以避免创建不必要的列表。

import re
from collections import Counter

with open("mails.txt", "r") as f:
    texte = f.read().split('\n')
    l=[]
    for line in texte:
        p=re.compile("(?<=@)[^.]+(?=\.)")
        newline = p.search(line)
        if(newline):

            newmail = newline.group(0)
            l.append(newmail)

Counter(l)

OUTPUT 输出值

Counter({'gmail': 4, 'yahoo': 3})

you can use split 您可以使用拆分

texte = "life@yahoo.com"
texte.split("@")
['life', 'yahoo.com']

do 2 splits. 做2分裂。 The second with @.. Then append the last item and apply the counter to the list 第二个带有@.。然后附加最后一项并将计数器应用于列表

import re
from collections import Counter

with open("mails.txt", "r") as f:
    texte = f.read().split('\n')

    domains = []

    for line in texte:
        line = line.split('@')
        if line[-1] != "":
            domains.append(line[-1])

mails_value = Counter(domains).most_common()

print(mails_value)   

[('gmail.com', 4), ('yahoo.com', 3)]

import re
from collections import Counter

mails = []

with open("mails.txt", "r") as f:
    texte = f.read().split()
    for i in texte:
        mails.append(re.search("@[\w.]+", i).group())

mails_value = Counter(mails).most_common()
print mails_value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM