python自定义方法处理日志文件
2021-05-15 18:29
标签:%s soscw text port write pat info not with open 从命令行界面拷贝的内容包含过个">>>",函数的作用是用正则把每两个">>>"之间的字符取出来,然后把包含“Traceback...”的字符的内容去掉,再写到另一个文件中 代码: #coding=utf-8 clearContent("d:\\re.txt") python自定义方法处理日志文件 标签:%s soscw text port write pat info not with open 原文地址:https://www.cnblogs.com/xiaxiaoxu/p/9750719.html
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 ""
结果:
上一篇:对java沙箱机制的一点了解
下一篇:java反射