0
import os
searchquery = 'word'
with open('Y:/Documents/result.txt', 'w') as f:
for filename in os.listdir('Y:/Documents/scripts/script files'):
with open('Y:/Documents/scripts/script files/' + filename) as currentFile:
for line in currentFile:
if searchquery in line:
start = line.find(searchquery)
end = line.find("R")
result = line[start:end]
print result
f.write(result + ' ' +filename[:-4] + '\n')
现在,这种方式很适合搜索“单词”并在单词后面打印所有内容,直到“R”提供它位于同一行上。但是,如果“R”在线上,它将不会打印之前的东西。Python在多行上搜索文本
例如:
this should not be printed!
this should also not be printed! "word" = 12345
6789 "R" After this R should not be printed either!
在6789第3行以上的情况下将不被打印与我的电流。但是我想要它。我如何让python继续多行,直到它到达“R”。
感谢您的帮助!