Reputation: 1289
I want to read a text file and check for strings
with open(my_file,'r') as f:
for line in f:
if 'text1' in line:
f.next()
if 'text2' in line:
# do some processing
I want to first find the text 'text1' at the beginning of the line then if found I want to check the next line for 'text2' if found then I will do some other processing. Appears that the f.next() is not moving to the next line.
Upvotes: 5
Views: 1715
Reputation: 77337
Use the for loop iteration and use a state variable to decide whether you are in the "test1" case. That way you don't have to juggle a separate StopIteration
exception.
text1_line = ""
with open(my_file) as f:
for line in f:
if text1_line and 'text2' in line:
# do some processing
text1_line = ""
continue
text1_line = line if 'text1' in line else ""
Upvotes: 3
Reputation: 106455
The variable line
does not magically get updated when you call f.next()
(or next(f)
in Python 3). You would instead have to assign the line returned by next
to a variable and test against it:
with open(my_file,'r') as f:
for line in f:
if 'text1' in line:
try:
next_line = next(f)
except StopIteration:
break # in case we are already at the end of file
if 'text2' in next_line:
# do some processing with line and next_line
If there can be cases where there are two consecutive lines with text1
followed by a line of text2
, however, calling next(f)
would make it consume the second line of text1
, making it unable to match text1
in the next iteration. To remedy that, you can use the pairwise
recipe in the itertools
documentation to iterate 2 lines at a time over the file instead:
import itertools
def pairwise(iterable):
a, b = itertools.tee(iterable)
next(b, None)
return zip(a, b)
with open(my_file,'r') as f:
for line, next_line in pairwise(f):
if 'text1' in line and 'text2' in next_line:
# do some processing with line and next_line
Upvotes: 3
Reputation: 71570
Try with enumerate
and readlines
:
with open(my_file,'r') as f:
lines = f.readlines() + ['']
for idx, line in enumerate(lines):
if 'text1' in line:
if 'text2' in lines[idx + 1]:
# do some processing
Upvotes: 2