嗨,我有一个代码,过滤一个特定单词的所有行('测试'),我想知道是否有人可以帮助解释如何过滤行不止一个单词,所以如果我有一个文件列出所有的过滤词和一个源文件,我将能够显示所有的源代码行,其中有任何过滤词。谢谢!你知道吗
def cat(openfile):
with open(openfile) as file:
return file.read()
def getlinewith(filecontents, containing):
for item in filecontents.split('\n'):
if containing in item:
yield item.strip()
matchedlines = []
for line in getlinewith(cat('C\\testdata_all.txt'), 'test'):
print(line)
matchedlines.append(line)
print(matchedlines)
可以使用
any()
和in
运算符:输出:
使用^{} :
相关问题 更多 >
编程相关推荐