python處理blog檔案資料__python

來源:互聯網
上載者:User

下面是Python資料處理的題目說明與要求:
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 are displayed in time order. Please write one script with Python language to support the following features: Sort the logs in alphabetical order of process name, e.g.: halserver, processman, etc. Filter the logs according to process name, the output only show the interested logs, e.g.: “procman”, and hiding the rest. Statistics the number of log lines for each process.

這是機頂盒啟動並執行blog文字檔,開啟後部分截圖如下:

一看很亂,其實不應該用微軟的txt開啟,嘗試用notepad++開啟後,結構清楚了很多,部分截圖如下:

下面給出代碼:
第1題的代碼如下:

#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)

第2,3題代碼如下:

#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)
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.