专家们,我试着在maillog文件中计算电子邮件地址和他们的代表的数量,我可以用正则表达式来计算(搜索)或者(重新匹配)但我希望这一切都能完成(关于芬德尔)目前我正在涉猎。。如有任何建议,不胜感激。。在
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
你可以考虑使用一个很好的命令表的熊猫,没有它可以考虑的结果。在
希望这能帮助。。。在
在关于芬德尔()将返回一个列表。查看How can I count the occurrences of a list item in Python?,有其他方法可以计算返回列表中的单词数。在
顺便说一下,计数器的有趣功能:
^{pr2}$因此,如果文件很大,我们可以计算每一行,并通过计数器将它们组合起来。在
我希望下面的代码能有所帮助。在
但是,有三点需要注意:
相关问题 更多 >
编程相关推荐