以下是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:

  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 only show the interested logs, e.g.: “procman”, and hiding the rest.
  3. 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[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=[] #定义列表存放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文件数据的更多相关文章

  1. Python读取SQLite文件数据

    近日在做项目时,意外听说有一种SQLite的数据库,相比自己之前使用的SQL Service甚是轻便,在对数据完整性.并发性要求不高的场景下可以尝试! 1.SQLite简介: SQLite是一个进程内 ...

  2. python读取EXCLE文件数据

    python读取EXCEL,利用 Google 搜索 Python Excel,点击第一条结果http://www.python-excel.org/ ,能够跨平台处理 Excel. 按照文档一步步去 ...

  3. python中读取文件数据时要注意文件路径

    我们在用python进行数据处理时往往需要将文件中的数据取出来做一些处理,这时我们应该注意数据文件的路径.文件路径不对,回报如下错误: FileNotFoundError: File b'..Adve ...

  4. python读取csv文件数据绘制图像,例子绘制天气每天最高最低气温气象图

  5. python将xml文件数据增强(labelimg)

    在处理faster-rcnn和yolo时笔者使用labelimg标注图片 但是我们只截取了大概800张左右的图,这个量级在训练时肯定是不够的,所以我们需要使用数据增强(无非是旋转加噪调量度)来增加我们 ...

  6. python使用h5py读取mat文件数据,并保存图像

    1 安装h5py sudo apt-get install libhdf5-dev sudo pip install h5py 假设你已经安装好python和numpy模块 2 读取mat文件数据 i ...

  7. python后端将svc文件数据读入数据库具体实现

    如何用python将svc文件的数据读入到MySQL数据库里,在此直接上代码了,感兴趣的朋友可以贴代码测试: import pandas as pd import os from sqlalchemy ...

  8. python操作txt文件中数据教程[4]-python去掉txt文件行尾换行

    python操作txt文件中数据教程[4]-python去掉txt文件行尾换行 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文章 python操作txt文件中数据教程[1]-使用pyt ...

  9. python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件

    python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 python操作txt文件中 ...

随机推荐

  1. python基础学习笔记——开发规范

    > 编码 1 2 3 4 5 所有的 Python 脚本文件都应在文件头标上     # -*- coding:utf-8 -*- 用于设置编辑器,默认保存为 utf-8 格式. > 注释 ...

  2. NYOJ 118 修路方案

    修路方案 时间限制:3000 ms  |  内存限制:65535 KB 难度:5   描述 南将军率领着许多部队,它们分别驻扎在N个不同的城市里,这些城市分别编号1~N,由于交通不太便利,南将军准备修 ...

  3. BNUOJ4359 无爱编号

    无爱编号 Time Limit: 2000ms Memory Limit: 65536KB   64-bit integer IO format: %lld      Java class name: ...

  4. mysql通过mysqldump实现备份,脚本编写

    每日三点同步mysql备份任务 crontab -e 0 3 * * * sh /home/shell/mysql_bakup.sh >> /dev/null 2>&1 my ...

  5. 【bzoj2229】[Zjoi2011]最小割 分治+网络流最小割

    题目描述 小白在图论课上学到了一个新的概念——最小割,下课后小白在笔记本上写下了如下这段话: “对于一个图,某个对图中结点的划分将图中所有结点分成两个部分,如果结点s,t不在同一个部分中,则称这个划分 ...

  6. 阐述struts2的执行流程。

    Struts 2框架本身大致可以分为3个部分:核心控制器FilterDispatcher.业务控制器Action和用户实现的企业业务逻辑组件. 核心控制器FilterDispatcher是Struts ...

  7. Java 模板权重随机

    Template templates=...// 所有的模板 final int _weights=1000; // 所有的模板权重 Template _template=null; //随机一个权重 ...

  8. 标准C程序设计七---02

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...

  9. iOS7中Cell高度 Label高度自适应

    
- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ NSStr ...

  10. MySQL的LOOP, LEAVE 和ITERATE语句(类似Continue、Break的写法)

    和REPEAT和while语句不同,LOOP.LEAVE.ITERATE更像其他编程语言中的goto语句. LOOP要设定一个label指定循环的开始位置,而LEAVE则像其他语言中的Break会离开 ...