在日志文件中创建包含特定单词的文本文件

时间:2013-09-25 18:57:12

标签: python

输入: - http://pastie.org/8355242

我正在尝试创建一个“.txt”文件,其中所有行都包含单词“wcnss_proc \ bt”(作为参数传递)和“Warning”在给定的输入文件“input.txt”中(如上面所示)链接)...我需要一个关于如何才能做到这一点的算法 我启动python编码..任何人都可以提供输入吗?

2 个答案:

答案 0 :(得分:1)

def filter_log(input_file, output_file, strs):
    with open(input_file, "r") as input, open(output_file, "w") as output:
        output.writelines(filter(lambda x: any([s in x for s in strs]), input.readlines()))

# here is just searched for "Warning", add other stuff
filter_log("input.txt", "output.txt", ["Warning"])

答案 1 :(得分:0)

您需要查找sys.argv值才能构建your_string

#pseudocode

good_lines = []
for line in open(filepath):
    if "your_string" in line:
      good_lines.append(line)

with open(new_file, 'w') as f:
  for line in good_lines:
      f.writeline(line)