码迷,mamicode.com
首页 > 编程语言 > 详细

python学习 根据日志中的ip和url排序

时间:2016-07-29 19:36:10      阅读:324      评论:0      收藏:0      [点我收藏+]

标签:python

#!/usr/bin/env python
#coding:utf-8

def open_file(file_name):
    res={}
    with open(file_name) as f:
        for line in f:
            tmp=line.split(‘ ‘)
#           print tmp
            ip,url=tmp[0],tmp[6]
#       print ip,url 以元组的形式存到列表中
            res[(ip,url)]=res.get((ip,url),0)+1
    return sorted(res.items(),key=lambda  x:x[1],reverse=True)
#print open_file(‘log1.log‘)


def get_htmlstr(arr):
#拼接字符串
    tmp1=‘<tr><td>Num%s</td> <td>%s</td> <td>%s</td> <td>%s</td></tr>‘
    html_str=‘<table border="1px">‘+tmp1%(‘名次‘,‘IP‘,‘URL‘,‘COUNT‘)
    for index,value in  enumerate(arr[:10]):
        html_str+=‘<tr><td>Num%s</td> <td>%s</td> <td>%s</td> <td>%s</td></tr>‘ %(index,value[0][0],value[0][1],value[1])
    html_str+=‘</table>‘
    return html_str
def write_html(file_name):
    res=open_file(file_name)
    with open(‘res2.html‘,‘w‘) as f:
        f.write(get_htmlstr(res))

write_html(‘log1.log‘)

技术分享

本文出自 “不抛弃!不放弃” 博客,谢绝转载!

python学习 根据日志中的ip和url排序

标签:python

原文地址:http://thedream.blog.51cto.com/6427769/1831648

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!