如何让while循环识别它是文件的结尾
How to get the while loop to recognise it is the end of the file
我有一个文本文件,其中的数据代表
1号线
姓名,电子邮件,phone
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
readline()
方法 returns 只有在 documentation 达到 EOF 时才为空字符串,因此您可以添加一个条件来检查 line
是否为空(不真实)在你打电话之后 readline()
:
with open(filename) as infile:
while True:
line = infile.readline()
if not line:
break
thisProfile = profile.Profile()
word = line.split()
thisProfile.set_given_name(word[0])
或者,您可以将文件对象用作带有 for
循环的迭代器:
with open(filename) as infile:
for line in infile:
thisProfile = profile.Profile()
word = line.split()
thisProfile.set_given_name(word[0])
您可以使用
for line in infiles.readline():
*code*
而不是 while 循环。
我有一个文本文件,其中的数据代表 1号线 姓名,电子邮件,phone 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
readline()
方法 returns 只有在 documentation 达到 EOF 时才为空字符串,因此您可以添加一个条件来检查 line
是否为空(不真实)在你打电话之后 readline()
:
with open(filename) as infile:
while True:
line = infile.readline()
if not line:
break
thisProfile = profile.Profile()
word = line.split()
thisProfile.set_given_name(word[0])
或者,您可以将文件对象用作带有 for
循环的迭代器:
with open(filename) as infile:
for line in infile:
thisProfile = profile.Profile()
word = line.split()
thisProfile.set_given_name(word[0])
您可以使用
for line in infiles.readline():
*code*
而不是 while 循环。