python - 如何在不检查空行的情况下在Python中循环到文件末尾?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29022377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to while loop until the end of a file in Python without checking for empty line?
提问by ay lmao
I'm writing an assignment to count the number of vowels in a file, currently in my class we have only been using code like this to check for the end of a file:
我正在写一个作业来计算文件中元音的数量,目前在我的班级中,我们只使用这样的代码来检查文件的结尾:
vowel=0
f=open("filename.txt","r",encoding="utf-8" )
line=f.readline().strip()
while line!="":
for j in range (len(line)):
if line[j].isvowel():
vowel+=1
line=f.readline().strip()
But this time for our assignment the input file given by our professor is an entire essay, so there are several blank lines throughout the text to separate paragraphs and whatnot, meaning my current code would only count until the first blank line.
但是这次我们的作业,我们教授给出的输入文件是一篇完整的文章,所以整个文本中有几个空行来分隔段落等等,这意味着我当前的代码只会计算到第一个空行。
Is there any way to check if my file has reached its end other than checking for if the line is blank? Preferably in a similar fashion that I have my code in currently, where it checks for something every single iteration of the while loop
除了检查该行是否为空之外,还有什么方法可以检查我的文件是否已到达结尾?最好采用与我目前有我的代码类似的方式,它会在 while 循环的每次迭代中检查某些内容
Thanks in advance
提前致谢
采纳答案by Adam Smith
Don't loop through a file this way. Instead use a for
loop.
不要以这种方式遍历文件。而是使用for
循环。
for line in f:
vowel += sum(ch.isvowel() for ch in line)
In fact your whole program is just:
事实上,你的整个程序只是:
VOWELS = {'A','E','I','O','U','a','e','i','o','u'}
# I'm assuming this is what isvowel checks, unless you're doing something
# fancy to check if 'y' is a vowel
with open('filename.txt') as f:
vowel = sum(ch in VOWELS for line in f for ch in line.strip())
That said, if you really want to keep using a while
loop for some misguided reason:
也就是说,如果你真的想while
因为一些误导的原因继续使用循环:
while True:
line = f.readline().strip()
if line == '':
# either end of file or just a blank line.....
# we'll assume EOF, because we don't have a choice with the while loop!
break
回答by Teresa Aysan
I discovered while following the above suggestions that for line in f: does not work for a pandas dataframe (not that anyone said it would) because the end of file in a dataframe is the last column, not the last row. for example if you have a data frame with 3 fields (columns) and 9 records (rows), the for loop will stop after the 3rd iteration, not after the 9th iteration. Teresa
我在遵循上述建议时发现 f: 中的行不适用于 Pandas 数据帧(不是任何人说的),因为数据帧中的文件末尾是最后一列,而不是最后一行。例如,如果您有一个包含 3 个字段(列)和 9 个记录(行)的数据框,for 循环将在第 3 次迭代后停止,而不是在第 9 次迭代后停止。特蕾莎
回答by Punnerud
Find end position of file:
查找文件的结束位置:
f = open("file.txt","r")
f.seek(0,2) #Jumps to the end
f.tell() #Give you the end location (characters from start)
f.seek(0) #Jump to the beginning of the file again
Then you can to:
然后你可以:
if line == '' and f.tell() == endLocation:
break