日志文件解析python [英] log file parsing python

查看:96
本文介绍了日志文件解析python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个任意行的日志文件.我所需要做的就是从日志文件中提取一行以"Total"开头的数据.我不希望文件中有其他任何行.

I have a log file with arbitrary number of lines. All I need is to extract is one line of data from the log file which starts with a string "Total". I do not want any other lines from the file.

如何为此编写一个简单的python程序?

How do I write a simple python program for this?

这是我的输入文件的外观

This is how my input file looks

TestName     id         eno            TPS      GRE          FNP
Test 1205    1            0            78.00        0.00         0.02
Test 1206    1            0            45.00        0.00         0.02
Test 1207    1            0            73400        0.00         0.02
Test 1208    1            0            34.00        0.00         0.02

Totals       64           0            129.61       145.64       1.12

我正在尝试获取看起来像这样的输出文件

I am trying to get an output file which looks like

TestName     id      TPS         GRE
Totals       64      129.61      145.64

好..所以我只希望输入文件中的第一,第二,第四和第五列,而不需要其他.我正在尝试list [index]实现此目的,但出现IndexError:(列表索引超出范围).另外2列之间的间距也不相同,所以我不确定如何拆分列并选择所需的列.有人可以帮我这个忙吗?下面是我使用的程序

Ok.. So I wanted only the 1st, 2nd, 4th and 5th column from the input file but not others. I am trying the list[index] to achieve this but getting a IndexError: (list index out of range ). Also the space between 2 columns are not the same so i am not sure how to split the columns and select the ones that i want. Can somebody please help me with this. below is the program I used

newFile = open('sana.log','r')

for line in newFile.readlines():

    if ('TestName' in line) or ('Totals' in line):

        data = line.split('\t')

        print data[0]+data[1]

推荐答案

theFile = open('thefile.txt','r')
FILE = theFile.readlines()
theFile.close()
printList = []
for line in FILE:
    if ('TestName' in line) or ('Totals' in line):
         # here you may want to do some splitting/concatenation/formatting to your string
         printList.append(line)

for item in printList:
    print item    # or write it to another file... or whatever

这篇关于日志文件解析python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆