93-re练习:匹配文件中指定模式

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))
    原文作者:凯茜的老爸
    原文地址: https://www.jianshu.com/p/203e4c7f52d3
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞