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')
现在这个方法可以很好地搜索“word”并打印单词后面的所有内容,直到一个“R”,前提是它在同一行上。然而,如果“R”在这条线上,它就不会打印之前的内容
例如:
this should not be printed!
this should also not be printed! "word" = 12345
6789 "R" After this R should not be printed either!
在上述情况下,第3行的6789将不会打印我的当前。不管我想怎样。如何使python不断地遍历多行,直到到达“R”
谢谢你的帮助
它不在下一行打印内容是正常的,因为您正在一行中搜索单词。更好的解决办法如下
请注意,这只适用于一次事故。您需要将其进一步分解以打印多个事件
相关问题 更多 >
编程相关推荐