log file parsing python

前端 未结 2 1776
滥情空心
滥情空心 2021-01-13 03:47

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

相关标签:
2条回答
  • 2021-01-13 04:34
    for line in open('filename.txt', 'r'):
        if line.startswith('TestName') or line.startswith('Totals'):
            fields = line.rsplit(None, 5)
            print '\t'.join(fields[:2] + fields[3:4])
    
    0 讨论(0)
  • 2021-01-13 04:37
    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
    
    0 讨论(0)
提交回复
热议问题