python自定义方法处理日志文件

时间:2023-12-18 15:50:14

从命令行界面拷贝的内容包含过个">>>",函数的作用是用正则把每两个">>>"之间的字符取出来,然后把包含“Traceback...”的字符的内容去掉,再写到另一个文件中

代码:

#coding=utf-8
import re
import os
def clearContent(fileName):
    result=[]
    with open(fileName) as fp:
        content=fp.read()
    L=re.findall( r'(?<=>>>).+?(?=>>>)' , content,re.M|re.DOTALL)
    print "len(L):",len(L)
    for i in L:
        if "Traceback" not in i:
            result.append(i)
    print "len(result):",len(result)
    with open("%s_new1.txt"%os.path.splitext(fileName)[0],"w") as fp1:
        for i in result:
            fp1.write(i)
    print "Done!\n please find the new file: %s_new1.txt"%os.path.splitext(fileName)[0]
    return ""

clearContent("d:\\re.txt")

结果:
python自定义方法处理日志文件