python处理blog文件数据
2017-07-18 21:15
316 查看
以下是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文本文件,打开后部分截图例如以下:
![](http://img.blog.csdn.net/20150606113942188)
一看非常乱,事实上不应该用微软的txt打开,尝试用notepad++打开后,结构清楚了非常多,部分截图例如以下:
![](http://img.blog.csdn.net/20150606114328872)
以下给出代码:
第1题的代码例如以下:
第2,3题代码例如以下:
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=[] #定义列表存放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)): #測试正则是否可行 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 )>0: list_process[m],list_process =list_process ,list_process[m] list1[m],list1 =list1 ,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=[] #定义列表存放Process list2=[] count=0 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)): #測试正则是否可行 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+=1 print count f2.writelines(list2)
相关文章推荐
- python处理blog文件数据
- ean13码的生成,python读取csv中数据并处理返回并写入到另一个csv文件中
- Python数据分析基础(四)——数据文件处理
- 【python图像处理】txt文件数据的读取与写入
- 数据处理_文件读取_PythonPickle模块(笔记)
- Python处理json格式的数据文件(一些坑、一些疑惑)
- python基础教程共60课-第33课处理文件中的数据
- python利用netCDF4处理气象数据(nc文件)
- Abaqus-利用python处理有多个instances的odb文件,得到inp模型数据用于前处理
- Python读取和处理文件后缀为.sqlite的数据文件(实例讲解)
- python处理大数据文件,set、enumerate使用,python传参,获取文件路径
- 【Python文件处理】递归批处理文件夹子目录内所有txt数据
- Python读取和处理文件后缀为".sqlite"的数据文件
- Python 对两个文件中数据的处理
- Python3 模拟MapReduce处理分析大数据文件——《Python宝典》
- 基因数据:使用Python处理vcf文件
- Head First Python 第二章 函数模块&第三章 文件与异常&第四章 持久存储&第五章 处理数据
- python数据分析-处理CSV/EXCEL表格文件
- python处理CSV文件格式数据
- 使用Python处理地理数据文件-多进程处理类