python解析日志的代码
2023-09-11 14:15:06 时间
#!/usr/bin/env python import re import os import os.path import gzip import json from urlparse import * import datetime import time DICT = {} def print_time(s): #print (datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')) print datetime.datetime.now().strftime("%H:%M:%S.%f") + " " + str(s) def remove_flag(s): return s.replace("[","").replace("]","") def remove_session_prefix(s): return s.replace("session:","") def parse_url(url): a_dict = {} aUrl = urlparse("http://"+url) url_params = dict([(k,v[0]) for k,v in parse_qs(aUrl.query).items()]) a_dict.update({"domain":aUrl.netloc}) a_dict.update({"path":aUrl.path}) a_dict.update({"params":url_params}) return a_dict def get_info(a_dict): new_dict = {} if a_dict["userid"] != "" and a_dict["userid"] != "0": new_dict.update({"userid":a_dict["userid"]}) else: new_dict.update({"userid":a_dict["session"]}) new_dict.update({"domain":a_dict["url"]["domain"]}) new_dict.update({"path":a_dict["url"]["path"]}) if "fr" in a_dict["url"]["params"]: new_dict.update({"fr":a_dict["url"]["params"]["fr"]}) else: new_dict.update({"fr":"-"}) if "ct" in a_dict["url"]["params"]: new_dict.update({"ct":a_dict["url"]["params"]["ct"]}) else: new_dict.update({"ct":"-"}) if "ac" in a_dict["url"]["params"]: new_dict.update({"ac":a_dict["url"]["params"]["ac"]}) else: new_dict.update({"ac":"-"}) key = json.dumps(new_dict) if key in DICT: DICT[key] += 1 #print key + " " + str(DICT[key]) else: DICT.update({key:1}) def read_logs(path): for item in os.listdir(path): f = gzip.open(path+"/"+item,"r") if f == None: raise "program can't open this file" i = 0 while True: if i % 10000 == 0:print_time(i) line = f.readline() if i % 10000 == 0:print_time("read to memory") if not line: break aDict = {} list = re.findall('\[.*?\]',line) if i % 10000 == 0:print_time("split to items") urlDict = parse_url(remove_flag(list[4])) if i % 10000 == 0:print_time("url fommat to dict") aDict.update({"userid":remove_flag(list[2])}) aDict.update({"session":remove_session_prefix(remove_flag(list[7]))}) aDict.update({"url":urlDict}) if i % 10000 == 0:print_time("create new dict") get_info(aDict) if i % 10000 == 0:print_time("save and diff") i += 1 f.close() if __name__ == "__main__": #try: read_logs("logs/20130908") f = open("data","w") for i in DICT: f.write(i+" "+str(DICT[i])) f.close() #except : # print "error"
相关文章
- 第二百九十八节,python操作redis缓存-Set集合类型,可以理解为不能有重复元素的列表
- Python操作Mysql实例代码教程在线版(查询手册)_python
- python之sys.stdout、sys.stdin以及设置打印到日志文件等
- python中日志logging模块的性能及多进程详解
- Python 日期和时间_python 当前日期时间_python日期格式化
- [Python] Simple Decorators
- python:pip升级pip本身和setuptools(Python 3.7.15)
- 两种方法,获取磁盘剩余空间--PYTHON
- python:利用logbook模块管理日志
- 日志回滚:python(日志分割)
- python下使用ElasticSearch
- linux执行python命令后没有反应,不打印日志信息
- python: easyocr的安装和使用(easyocr 1.6.2 / Python 3.7.15 )
- 强化学习笔记:基于价值的学习之价值计算(python实现)
- Atitit php java python nodejs错误日志功能的比较
- 华为OD机试 - 最多颜色的车辆(Java & JS & Python)
- Python:利用python代码编程实现将视频的avi格式转换为MP4格式
- Python之ffmpeg-python:ffmpeg-python库的简介、安装、使用方法之详细攻略
- Python语言学习:python语言代码调试—异常处理之详细攻略
- Python语言学习:在python中,如何获取变量的本身字符串名字而非其值/内容及其应用(在代码中如何查找同值的所有变量名)
- Python:利用python编程实现三维图像绘制展示(六面体旋转、三维球柱状体、下雪场景等)
- Python语言学习:基于python五种方法实现使用某函数名【func_01】的字符串格式('func_01')来调用该函数【func_01】执行功能
- 已解决2.Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and wi
- 学习python第一天,请教一下怎么学?
- 〖Python零基础入门篇(57)〗- Python中的日志模块
- python基础知识之 Python代码规范
- Python日志模块学习,从这里开始...
- 分享10个超级实用的Python自动化脚本
- Python实现ABC人工蜂群优化算法优化支持向量机回归模型(SVR算法)项目实战
- 【华为机试真题 Python实现】消消乐游戏【2022 Q1 Q2 | 100分】
- 【 华为OD机试 2023】 日志限流(C++ Java JavaScript Python)
- Python爬虫:PyExecJS在python中运行javascript代码
- Python 23种设计模式全(python例子)
- Python代码集----Python日志系统代码
- Twisted twisted.python.log日志
- Python可视化数据分析01、python环境搭建
- Python可视化数据分析05、Pandas数据分析