以下是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. prometheus + mysqld_exporter + grafana 实现对mysql db的监控

    https://blog.csdn.net/hfut_wowo/article/details/78536022 1.参考这篇博文2.博主的用的是windows版本 prometheus-2.5.0- ...

  2. Python第三方库之openpyxl(5)

    Python第三方库之openpyxl(5) 气泡图 气泡图类似于散点图,但使用第三个维度来确定气泡的大小,图表可以包括多个项目 from openpyxl import Workbook from ...

  3. WordPress登录框显示/隐藏输入的密码

    直接让用户自行根据需要选择是全部隐藏输入的密码,还是全部显示输入的密码. 在全部显示密码框的内容时,用户输错的可能性就大大降低,这也是微软推荐的一种密码框处理方式.效果如下: 今天,我将给大家介绍,在 ...

  4. 北京师范大学第十五届ACM决赛-重现赛

    Another Server 时间限制:1秒 空间限制:262144K 题目描述 何老师某天在机房里搞事情的时候,发现机房里有n台服务器,从1到n标号,同时有2n-2条网线,从1到2n-2标号,其中第 ...

  5. ICMP TYPE CODE

    TYPE CODE Description Query Error 0 0 Echo Reply——回显应答(Ping应答) x   3 0 Network Unreachable——网络不可达   ...

  6. USACO Longest Prefix

    题目大意:给出一个长字符串,问最长的前缀,使得这个前缀能用给出的一些元素组合而成 思路:暴力dp,dp[i]表示长度为i的前缀能否被表示 /*{ ID:a4298442 PROB:prefix LAN ...

  7. poj2945 Find the Clones

    Find the Clones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 8490   Accepted: 3210 D ...

  8. 【bzoj4320】【ShangHai2006 Homework】【并查集+离线处理】

    ShangHai2006 Homework Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 918  Solved: 460[Submit][Statu ...

  9. idea16使用maven命令clean、编译、打包jar或者war

    项目环境:idea16+jdk1.7+maven-3.3.9 项目说明:编写简单的java类,使用maven命令生成jar包,然后执行------->"java  -classpath ...

  10. OC-Runtime温故知新

    每个java应用程序都有一个runtime类实例,使应用程序能够与其运行的环境相连接.可以通过getRuntime 方法获取当前运行时,应用程序不能自己创建runtime类实例.Runtime 没有构 ...