我正在处理一个包含一万两千行代码的输出日志文件,其中大多数包含如下内容:
"760.0132 EXP window1: blendMode = 'avg'"
我的目标是完全删除所有具有“ EXP window1:blendMode ='avg'”的行。我可以从找到文本的所有行中删除该文本位,但不能删除数字。这是我用来删除文本位(从另一个堆栈溢出问题/答案中借来的)的代码:
infile = "01_Day1_run1.txt"
outfile = "01_Day1_run1_cleaned.txt"
delete_list = [" EXP window1: blendMode = 'avg'"]
fin = open(infile)
fout = open(outfile, "w+")
for line in fin:
for word in delete_list:
line = line.replace(word, "")
fout.write(line)
fin.close()
fout.close()
我希望我可以添加类似的内容
delete_list = ["1**.**** EXP window1: blendMode = 'avg'"]
以便删除包含所有文本的任何数字,以及该行中的任何数字,但似乎不起作用。对于如何最好地清理日志文件的任何建议,将不胜感激。
非常感谢, 西蒙
答案 0 :(得分:0)
infile = "01_Day1_run1.txt"
outfile = "01_Day1_run1_cleaned.txt"
delete_list = [" EXP window1: blendMode = 'avg'"]
fin = open(infile)
fout = open(outfile, "a")
for line in fin:
for word in delete_list:
if word in line:
wordCheck = False
break
else:
wordCheck = True
if wordCheck:
fout.write(line)
fin.close()
fout.close()
也许更清洁:
with open("01_Day1_run1.txt", "r") as infile, open("01_Day1_run1_cleaned.txt", "a") as outfile:
for line in infile:
if not any(filter in line for filter in delete_list ):
outfile.write(line)
答案 1 :(得分:0)
infile = "01_Day1_run1.txt"
outfile = "01_Day1_run1_cleaned.txt"
delete_string = "EXP window1: blendMode = 'avg'"
fin = open(infile)
fout = open(outfile, "a")
for line in fin.readLines():
if delete_list not in line:
fout.write(line)
fin.close()
fout.close()
答案 2 :(得分:0)
为什么要使用Python做到这一点?您可以使用简单的grep -v
或findstr /V
进行此操作,如以下示例所示:
Prompt>grep -v "blendmode" input.txt >output.txt