93-re练习:匹配文件中指定模式
作者:
凯茜的老爸 | 来源:发表于
2018-08-04 13:42 被阅读0次import re
def count_patt(fname, patt):
cpatt = re.compile(patt)
result = {}
with open(fname) as fobj:
for line in fobj:
m = cpatt.search(line) # 如果匹配不到,返回None
if m:
key = m.group()
result[key] = result.get(key, 0) + 1
return result
if __name__ == '__main__':
fname = 'access_log' # apache日志文件
ip = '^(\d+\.){3}\d+' # 日志开头的ip地址
print(count_patt(fname, ip))
br = 'Firefox|MSIE|Chrome' # 日志中客户端浏览器
print(count_patt(fname, br))
本文标题:93-re练习:匹配文件中指定模式
本文链接:https://www.haomeiwen.com/subject/cdcmvftx.html
网友评论