Reputation: 5
I am having some trouble with my code for an assignment where I am supposed to go through a text file and find birthdays that are in april and print them. Here is what the text file looks like:
Bob, June 10
Joe, April 12
Sue, July 22
I am supposed to go through it and print out the name and birthdays in april but I keep getting e = next(a) StopIteration. I am really confused!
a = open("c:/Users/me/Documents/fruits.txt", "r")
for k in a:
e = next(a)
b = e.strip()
c = b[0 : 5]
if c == "April":
print b
e = next(a)
else:
e = next(a)
a.close()
Upvotes: 1
Views: 1509
Reputation: 194
This code will iterate through the lines of the file and print any line with "April" in it. When you are iterating through the lines of a file, you do not need to call next() in the loop body.
for line in open("fruits.txt"):
if "April" in line:
print(line)
Upvotes: 2