Python Processing Blog File data

Source: Internet
Author: User

The following is the Python Data processing topic description and Requirements:
The attachment is a log file used to show running status of Set-top-box, and each line in the file follows the format of " LineNumber + time + ProcessName + (ProcessID) + Logs ", currently the Logs is displayed in time order. Write one script with Python language to support the following features:

    1. Sort the logs in alphabetical order of process name, e.g.: Halserver, Processman, etc.
    2. Filter the logs according to process name, the output is only show the interested logs, e.g.: "Procman", and hiding the rest.
    3. Statistics the number of log lines for each process.

This is the set-top box to run the blog text file, open the following sections as follows:

A look very messy, in fact, should not be opened with the txt of Microsoft, try to open with notepad++, the structure is clear a lot, part of the following:

The following code is given:
The code for question 1th is as follows:

#coding=utf-8                                import re  f1=open(‘stblog.txt‘,‘r‘)f2=open(‘cc1.txt‘,‘w‘)list1=f1.readlines()list_process=[]    #定义列表存放Processres=‘\d\D\d\d:\d\d:\d\d\.\d{3}\s([a-z]+)‘for i in range(len(list1)):    list_process.append(re.findall(res,str(list1[i])))for i in range(len(list_process)):  #测试正则是否可行    if len(list_process[i])>1:        print ‘zheng ze fail‘#print len(list_process)    #print len(list1)#print list_process[141]#print list1[141]for m in range(len(list1)):      #冒泡排序    for n in range(m+1,len(list1)):        if cmp(list_process[m],list_process[n])>0:            list_process[m],list_process[n]=list_process[n],list_process[m]            list1[m],list1[n]=list1[n],list1[m]f2.writelines(list1)

2nd, the 3 question code is as follows:

#coding=utf-8                              import re  f1=open(‘stblog.txt‘,‘r‘)f2=open(‘cc2.txt‘,‘w‘)list1=f1.readlines()list_process=[]      #定义列表存放Processlist2=[]count=0res=‘\d\D\d\d:\d\d:\d\d\.\d{3}\s([a-z\.\-]+)‘for i in range(len(list1)):    list_process.append(re.findall(res,str(list1[i])))for i in range(len(list_process)):  #测试正则是否可行    if len(list_process[i])>1:        print ‘zheng ze fail‘s=raw_input("please input the log you interested:")for i in range(len(list_process)):    if list_process[i]==s.split():        list2.append(list1[i])   #将对应的process行添加到cc2.txt        count+=1print countf2.writelines(list2)

Python Processing Blog File data

Related Article

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.