gpt4 book ai didi

python - 如何grep一行中的多个单词?

转载 作者:行者123 更新时间:2023-12-01 05:36:09 34 4
gpt4 key购买 nike

我有以下代码,我试图在作为第一个参数传递的给定输入文件中 grep 查找“警告”“wcnss_proc\bt”,但是,output.txt 仅 grep “警告”...可以指出下面的代码中有什么问题吗?我想对输入文件中的所有行查找“警告”和“wcnss_proc\bt”

import sys

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(sys.argv[1], "output.txt", ["Warning","wcnss_proc\bt"])

最佳答案

\b 是转义字符。你的意思是\\b吗?

关于python - 如何grep一行中的多个单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19015343/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com