There is a solution for searching backward inline in Python Reverse Find in String:
s.rfind('I', 0, index)
But if I need to search for a string in several lines above that line? Say I have found the keyword by using:
with open("file.txt") as f
searchlines = f.readlines()
for i, line in enumerate(searchlines):
if "keyword" in line:
do_something()
I want do_something()
is to find another keyword backward. To apply the code above, I think I need to f.read()
so that I can make the file as a string. But this is totally nut, since I have to readlines()
and read()
the (large) file. I need to use readlines()
because the first keyword may appears several times in the text, and I need to find them all.
Is there any better way to do this?
@engineer
- kỹ sư
@engineering
- kỹ thuật
- civil e. ngành xây dựng
- communication e. kỹ thuật thông tin
- control e. kỹ thuật [điều chỉnh, điều khiển] (tự động)
- development e. nghiên cứu những kết cấu mới