以下是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. 关于springmvc返回json格式数据

    1.引入maven依赖 <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifac ...

  2. iOS WKWebView

    Webkit 是 iOS 8.0 后提供的新的框架,组件WKWebView比较UIWebView 速度更快.占用内存更少了,可支持性更多 WKWebView可通过KVO监听属性 title.estim ...

  3. 【JavaScript 12—应用总结】:弹出登录框

    导读:上篇博客中,做好了个人中心的下拉菜单,这次,将做每个网站都会有的一个登录功能,以此类推,可以做出别的想要的弹出框,如错误提示啦,或者注册. 一.实现分析 首先:和下拉菜单一样,需要通过CSS样式 ...

  4. EasyUI 动态生成列加分页

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat=&qu ...

  5. iOS代理转移

    在控制器里面想调用一个视图的子视图的代理方法很简单 1.找到子视图的协议和代理属性 2.给你想调用的控件添加代理属性,遵守的协议和子视图的一样 3.重写代理属性的set方法

  6. FZU 2020 :组合 【lucas】

    Problem Description 给出组合数C(n,m), 表示从n个元素中选出m个元素的方案数.例如C(5,2) = 10, C(4,2) = 6.可是当n,m比较大的时候,C(n,m)很大! ...

  7. C/C++ 程序中调用命令行命令并获取命令行输出结果

    在 c/c++ 程序中,可以使用 system()函数运行命令行命令,但是只能得到该命令行的 int 型返回值,并不能获得显示结果.例如system(“ls”)只能得到0或非0,如果要获得ls的执行结 ...

  8. N皇后-位运算优化

    N皇后问题 时间限制: 5 Sec  内存限制: 128 MB 题目描述 魔法世界历史上曾经出现过一个伟大的罗马共和时期,出于权力平衡的目的,当时的政治理论家波利比奥斯指出:“事涉每个人的权利,绝不应 ...

  9. 森林 BZOJ 3123

    题解: 第k大直接用主席树解决 合并利用启发式合并,将较小的连接到较大的树上 #include<cmath> #include<cstdio> #include<cstd ...

  10. Ajax && json(原始)

    function findSend() { $.ajax({ // async: false, // cache: true, type: "POST", url: "I ...