[英]Saving an index from a list in a while loop in Python
如何在“ while”循環中保存列表的索引?
基本上,我正在瀏覽一個文本文件,我想標識一個日期,然后再獲取與該日期相對應的信息,即下面的行。 到目前為止,我可以將輸入到該行的日期與該日期匹配,但是不知道如何保存該行,因此我可以從下面的行中獲取他的信息。
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")
如何在“ while”循環中保存列表的索引。 基本上,我正在瀏覽一個文本文件,我想標識一個日期,然后再獲取與該日期相對應的信息,即下面的行。 到目前為止,我可以將輸入到該行的日期與該日期匹配,但是不知道如何保存該行,因此我可以從下面的行中獲取他的信息。
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
您必須手動跟蹤索引,即:
i = 0
while True:
# Do stuff.
i += 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.