我有一个任意行数的日志文件.我只需要从日志文件中提取一行数据,该行以字符串"Total"开头.我不想要文件中的任何其他行.
我如何为此编写一个简单的python程序?
这是我的输入文件的外观
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
我试图得到一个看起来像的输出文件
TestName     id      TPS         GRE
Totals       64      129.61      145.64
好的..所以我只希望输入文件中的第1,第2,第4和第5列而不是其他列.我正在尝试使用list [index]来实现这一点,但得到一个IndexError :(列表索引超出范围).2列之间的空间也不一样,所以我不知道如何拆分列并选择我想要的列.有人可以帮我这个.以下是我使用的程序
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
| 归档时间: | 
 | 
| 查看次数: | 22468 次 | 
| 最近记录: |