【发布时间】:2018-10-27 13:10:42
【问题描述】:
我有一个文本文件,其中的数据代表 1号线 姓名、电子邮件、电话 2号线 地址 3号线 是一个整数,表示列出了多少朋友。 4号线~n 每行包含一个名称
我已经能够得到每一行
line = infile.readline()
当我到达文件末尾时遇到问题。
它想再次循环,好像它无法识别文件的结尾
infile = open(filename, "r")
lines = infile.readlines()
with open(filename) as infile:
line = infile.readline()
count = 1
while line:
thisProfile = profile.Profile()
if count > 1:
line = infile.readline()
count += 1
word = line.split()
thisProfile.set_given_name(word[0])
thisProfile.set_last_name(word[0])
thisProfile.set_email_name(word[0])
thisProfile.set_phone(word[0])
## LINE 2 -- ADDRESS
line = infile.readline()
count += 1
thisProfile.set_address(line)
## LINE 3 -- GET FRIEND COUNT
line = infile.readline()
count += 1
## get all the friends and add them to the friends_list
friendCount = int(line)
thisProfile.set_number_friends(friendCount)
friendList = []
if friendCount > 0:
for allfreinds in range(0,friendCount):
line = infile.readline()
count += 1
friendList.append(line)
thisProfile.set_friends_list(friendList)
friends.txt
John Doe john.doe@hotmail.com 123456789
1 alltheway ave
1
Jane Doe
Paul Andrews paul.andrews@gmail.com 987654321
69 best street
0
Jane Doe jane.doe@facebook.com 159753456
1 alltheway ave
2
John Doe
Paul Andrews
【问题讨论】:
-
我更新了标题,我的具体问题是 END_OF_FILE .. 它想循环回到开头。除了在开始时进行测试 if lineCount 之外,还有其他方法可以阻止这种情况的发生吗
标签: python loops while-loop readline eof