Here is the description and requirements for Python data processing:
The attachment is a log file used to show running status of Set-top-box, and the "the" of the file follows the format of LineNumber + time + processname + (ProcessID) + Logs "Currently the Logs are into time order. "Write one script with Python language to support" following Features:sort the logs in alphabetical order of Pro Cess name, e.g.: Halserver, Processman, etc. Filter the logs according to process name, the output is show the interested logs, e.g.: "Procman", and hiding the rest. Statistics the number of log lines for each process.
This is the set-top box running blog text file, open the following section screenshot as follows:
A look very messy, in fact, should not use the Microsoft TXT open, try to open with notepad++, structure clear a lot, some screenshots as follows:
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=[] #定义列表存放Process
res= ' \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)): # Test whether it is feasible 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 code is as follows:
#coding =utf-8 Import re f1=open (' Stblog.txt ', ' R ') f2=open (' Cc2.txt ', ' W ') List1=f1.rea Dlines () list_process=[] #定义列表存放Process list2=[] count=0 res= ' \d\d\d\d:\d\d:\d\d\.\d{3}\s ([a-z\.\-]+) ' For I in Ran
GE (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 to interested:") for I I
N Range (len (list_process)): If List_process[i]==s.split (): List2.append (List1[i]) #将对应的process行添加到cc2. txt Count+=1 Print Count F2.writelines (list2)