python处理日志文件和剥离字符

python处理日志文件和剥离字符,python,Python,我正在制作一个快速日志分析工具: findme = 'important ' logf = file('new.txt') newlines = [] for line in logf: if findme in line: line.partition("as follows: ")[2] newlines.append(line) outfile = file('out.txt', 'w') outfile.wr

我正在制作一个快速日志分析工具:

findme = 'important '
logf = file('new.txt')
newlines = []

    for line in logf:
        if findme in line:
            line.partition("as follows: ")[2]
            newlines.append(line) 


outfile = file('out.txt', 'w')
outfile.writelines(newlines)

我不知道该如何使用分区之类的方法来删除文本“如下:”以及每行之前的所有内容。我没有收到任何错误,但我试图剥离的文本仍保留在输出中。

另外,我对行有点困惑

line.partition("as follows: ")[2]
。它什么也不做。也许你想要

line = line.partition("as follows")[2]
??顺便说一句,最好是只在for循环中写入每一行,而不是在最后写入一个巨大的
writelines
。您当前的解决方案将为大文件使用大量内存,而不适用于无限文件

最终版本如下所示:

findme = 'important '
outfile = open('out.txt', 'w')
for line in open('new.txt'):
    if findme in line:
        outfile.write(line.partition('as follows: ')[2])
这是正则表达式

import re

findme = 'important ' 
pat = re.compile('.*(%s)?.*as follows: ((?(1).*\n|.*%s.*\n))' % (findme,findme))

with open('new.txt','r') as logf, open('out.txt','w') as outfile:
    for line in logf:
        m = pat.match(line)
        if m: outfile.write( m.group(2) )
其优点是,它可以搜索更多特定的项目,而不仅仅是使用if findme in line'指令
例如,使用
findme=”(?不要使用
file
use
open
),这看起来甚至不像是语法有效的Python。