[英]Python: reading text, readlines() vs iter()
I have a text file, with blocks of text, each separated by a line break. 我有一个文本文件,其中包含文本块,每个文本块之间都由一个换行符分隔。 There are
M
blocks, each with N
lines of text. 有
M
个块,每个块有N
行文本。 I now want to read n
lines for each of m
blocks, where n<=N
and m<=M
. 我现在想为
m
个块中的每个块读取n
行,其中n<=N
和m<=M
I have tried something similar to the following: 我已经尝试过类似以下内容:
num_blocks = 4 # or whatever value I choose
num_lines = 3 # or whatever value I choose
with open('file.txt', 'r') as f:
lines = f.readlines()
block_num = 0
line_num = 0
for line in lines:
# do something with the line .....
line_num += 1
# has the specified number of lines been read?
if line_num == num_lines:
block_num += 1
# has the specified number of blocks been read?
if block_num == num_blocks:
break;
else:
line_num = 0
However, when n<N
, I need to skip over the remaining lines in the current block. 但是,当
n<N
,我需要跳过当前块中的其余行。 I have tried putting 我试过了
if line != '\n':
continue
next to # do something with the line .....
, but this then skips the entire first block. # do something with the line .....
旁边# do something with the line .....
,但这会跳过整个第一个程序段。
Alternatively, I have tried creating an iterator it = lines.iter()
and incrementing each one accordingly. 另外,我尝试创建一个迭代器
it = lines.iter()
并相应地递增每个迭代器。 The problem with this approach is that there is no way of knowing when the end of the file has been reached. 这种方法的问题是无法知道何时到达文件末尾。
readlines()
does this for me, but I don't know how to know when the last line has been reached if I am using an interator. readlines()
为我完成了此操作,但如果使用中介程序,我不知道如何知道何时到达最后一行。
Any help? 有什么帮助吗? Thanks!
谢谢!
You can test for the end of an iterator like so: 您可以像这样测试迭代器的结尾:
try:
it.next()
except StopIteration:
*do something*
The StopIteration Exception is what an iterator throws when there is nothing left to iterate. StopIteration Exception是在没有任何要迭代的情况下抛出的迭代器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.