How do you save an index of a list while in a 'while' loop?
Basically I am running through a text file and I want to identify a date, and then afterwards get the information that corresponds to that date which is the line below. So far I can match the date entered to the line with that date but don't know how to save that line so I can get he information from the line below.
f = open("studentinfo.txt")
#ask for date
d = input("Enter the date:")
date = [d]
#check to see if date is avialable
while True:
line = f.readline().split()
if line:
if line == date:
print(line)
#save data in here, index etc
else:
break
print("No data available")
How do you save an index of a list while in a 'while' loop. Basically I am running through a text file and I want to identify a date, and then afterwards get the information that corresponds to that date which is the line below. So far I can match the date entered to the line with that date but don't know how to save that line so I can get he information from the line below.
f = open("studentinfo.txt")
#ask for date
d = input("Enter the date:")
date = [d]
#check to see if date is avialable
for i, line in enumerate(f.readlines()):
if line:
if line == date:
print(line)
#save data in here, index etc
else:
break
# print("No data available") <-- check indents unreachable code
You have to track the index manually, ie:
i = 0
while True:
# Do stuff.
i += 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.