Python learns to sort by IP and URL in the log

Source: Internet
Author: User

#!/usr/bin/env python#coding:utf-8def 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  Save as a tuple in the list              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&lT;/td></tr> '     html_str= ' <table border= "1px" > ' +tmp1% (' rank ', ' 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_strdef  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 ')

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/85/26/wKioL1ea_-fyxHYIAABLj-jxpwI996.png "title=" m0xuwx (ts]6afypd$pll9b0.png "alt=" Wkiol1ea_-fyxhyiaablj-jxpwi996.png "/>

This article from "Do not abandon!" Do not give up "blog, declined reprint!"

Python learns to sort by IP and URL in the log

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.