【问题标题】:Dictionary to count the email-address from the maillog file python [closed]从邮件日志文件python计算电子邮件地址的字典[关闭]
【发布时间】:2016-03-30 07:52:05
【问题描述】:

专家,我正在尝试计算邮件日志文件中的电子邮件地址和他们的重复次数,我可以使用正则表达式(re.search)或(re.match)以某种方式进行计算,但我正在寻找这个与我目前正在涉足的(re.findall)一起完成......希望有任何建议......

1) 代码行 ...

# cat maillcount31.py
#!/usr/bin/python
import re
#count = 0
mydic = {}
counts = mydic
fmt = " %-32s %-15s"
log =  open('kkmail', 'r')

for line in log.readlines():
        myre = re.search('.*from=<(.*)>,\ssize', line)
        if myre:
           name = myre.group(1)
           if name not in mydic.keys():
              mydic[name] = 0
           mydic[name] +=1

for key in counts:
   print  fmt % (key, counts[key])

2) Output from the Current code..

# python maillcount31.py
 root@MyServer1.myinc.com         13
 User01@MyServer1.myinc.com       14

【问题讨论】:

  • 您没有确切说明问题出在哪里,因此在不分析代码的情况下,我想说您想要两件事:1. 一个遍历所有电子邮件地址外观的函数,以及2. collections.Counter 计算它们。

标签: python


【解决方案1】:

希望对您有所帮助...

from collections import Counter
emails = re.findall('.*from=<(.*)>,\ssize', line)# Modify re according to your file pattern  OR line pattern. If findall() on each line, each returned list should be combined.
result = Counter(emails)# type is <class 'collections.Counter'>
dict(result)#convert to regular dict

re.findall() 将返回一个列表。查看How can I count the occurrences of a list item in Python?,还有其他方法可以计算返回列表中的单词。

顺便说一下Counter的有趣功能:

>>> tmp1 = Counter(re.findall('from=<([^\s]*)>', "from=<usr1@gmail.com>, from=<usr2@gmail.com>, from=<usr1@gmail.com>, from=<usr1@gmail.com>, from=<usr1@gmail.com>,") )
>>> tmp1
Counter({'usr1@gmail.com': 4, 'usr2@gmail.com': 1})
>>> tmp2 = Counter(re.findall('from=<([^\s]*)>', "from=<usr2@gmail.com>, from=<usr3@gmail.com>, from=<usr1@gmail.com>, from=<usr1@gmail.com>, from=<usr1@gmail.com>,") )
>>> dict(tmp1+tmp2)
{'usr2@gmail.com': 2, 'usr1@gmail.com': 7, 'usr3@gmail.com': 1}

所以,如果文件很大,我们可以统计每一行,并借助 Counter 将它们组合起来。

【讨论】:

【解决方案2】:

您是否考虑过使用 pandas,它可以为您提供一个漂亮的结果表,而无需使用正则表达式命令。

 import pandas as pd

 emails = pd.Series(email_list)
 individual_emails = emails.unique()

 tally = pd.DataFrame( [individual_emails , [0]*len(individual_emails)] )
 #makes a table with emails and a zeroed talley

 for item in individual_emails.index:
      address = tally.iloc[item,0]
      sum = len(email[email==address])

      tally.iloc[item,1] = sum


 print tally

【讨论】:

  • @ user2589273 - 我没有安装 pandas 模块。虽然感谢您的帮助,但一旦我有熊猫可用,我会在稍后尝试。
【解决方案3】:

我希望底部的代码有所帮助。

但是,这里一般要注意三点:

  1. 打开文件时使用 (with)
  2. 迭代字典时,使用iteritems()
  3. 使用容器时,collections 是您最好的朋友

#!/usr/bin/python
import re
from collections import Counter 

fmt = " %-32s %-15s"
filename = 'kkmail'

# Extract the email addresses
email_list = []
with open(filename, 'r') as log:
   for line in log.readlines():
      _re = re.search('.*from=<(.*)>,\ssize', line)
         if _re:
            name = _re.group(1)
            email_list.append(name)

# Count the email addresses
counts = dict(Counter(email_list)) # List to dict of counts: {'a':3, 'b':7,...}
for key, val in counts.iteritems():
   print  fmt % (key, val)

【讨论】:

  • @ Hill - 感谢您的代码 .. 这是有效的,但我正在使用 re.compile 使用 re.findall() 函数,因此,我们可以编译一次正则表达式,而不是在文件。虽然我发布的代码工作正常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-26
  • 2012-02-17
  • 2016-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-16
相关资源
最近更新 更多