python处理blog文件数据
以下是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文本文件,打开后部分截图例如以下:
一看非常乱,事实上不应该用微软的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文件数据的更多相关文章
- Python读取SQLite文件数据
近日在做项目时,意外听说有一种SQLite的数据库,相比自己之前使用的SQL Service甚是轻便,在对数据完整性.并发性要求不高的场景下可以尝试! 1.SQLite简介: SQLite是一个进程内 ...
- python读取EXCLE文件数据
python读取EXCEL,利用 Google 搜索 Python Excel,点击第一条结果http://www.python-excel.org/ ,能够跨平台处理 Excel. 按照文档一步步去 ...
- python中读取文件数据时要注意文件路径
我们在用python进行数据处理时往往需要将文件中的数据取出来做一些处理,这时我们应该注意数据文件的路径.文件路径不对,回报如下错误: FileNotFoundError: File b'..Adve ...
- python读取csv文件数据绘制图像,例子绘制天气每天最高最低气温气象图
- python将xml文件数据增强(labelimg)
在处理faster-rcnn和yolo时笔者使用labelimg标注图片 但是我们只截取了大概800张左右的图,这个量级在训练时肯定是不够的,所以我们需要使用数据增强(无非是旋转加噪调量度)来增加我们 ...
- python使用h5py读取mat文件数据,并保存图像
1 安装h5py sudo apt-get install libhdf5-dev sudo pip install h5py 假设你已经安装好python和numpy模块 2 读取mat文件数据 i ...
- python后端将svc文件数据读入数据库具体实现
如何用python将svc文件的数据读入到MySQL数据库里,在此直接上代码了,感兴趣的朋友可以贴代码测试: import pandas as pd import os from sqlalchemy ...
- python操作txt文件中数据教程[4]-python去掉txt文件行尾换行
python操作txt文件中数据教程[4]-python去掉txt文件行尾换行 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文章 python操作txt文件中数据教程[1]-使用pyt ...
- python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件
python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 python操作txt文件中 ...
随机推荐
- HP DL380 G5 安装操作系统流程
1.准备服务器引导盘,设置光盘启动.注意:会将服务器所有数据清除 2.依据引导盘选择需要的安装系统 3.插入系统安装光盘.ps:若kvm等远程工具无法插入光盘,需要插入物理介质的安装光盘
- 【LeetCode】Palindrome Number(回文数)
这道题是LeetCode里的第9道题. 题目说的: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: ...
- numpy hstack()
numpy.hstack(tup)[source] Stack arrays in sequence horizontally (column wise). Take a sequence of ar ...
- ubuntu14.04:php7+apache2+mysql
apache2: sudo apt-get install apache2 apache2-dev service apache2 start mysql: sudo apt-get install ...
- nginx进程属主问题讨论
文章作者:luxianghao 文章来源:http://www.cnblogs.com/luxianghao/p/6107096.html 转载请注明,谢谢合作. 免责声明:文章内容仅代表个人观点,如 ...
- POJ——1364King(差分约束SPFA判负环+前向星)
King Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11946 Accepted: 4365 Description ...
- BZOJ2780 [Spoj]8093 Sevenk Love Oimaster 【广义后缀自动机】
题目 Oimaster and sevenk love each other. But recently,sevenk heard that a girl named ChuYuXun was dat ...
- 刷题总结——随机图(ssoi)
题目: 随机图 (random.cpp/c/pas) [问题描述] BG 为了造数据,随机生成了一张�个点的无向图.他把顶点标号为1~�. 根据BG 的随机算法,对于一个点对�, �(1 ≤ � &l ...
- wamp Apache和mysql服务无法启动的终极解决方法!!!!!!
用了几年的wampserver 突然宣告无法启动,Apache和mysql都崩溃了,在计算机的服务选项里面也是无法启动的,系统报了一个未知错误,重装了N个版本的PHP集成开发环境,都宣告失败! 我想应 ...
- Python入门--13--爬虫一
URL的格式一般为(带方括号的是可选的): protocol://hostname[:port]/path/[;parameters][?query]#fragment URL由三部分组成: 第一部分 ...