[英]Python: reading text, readlines() vs iter()
我有一个文本文件,其中包含文本块,每个文本块之间都由一个换行符分隔。 有M
个块,每个块有N
行文本。 我现在想为m
个块中的每个块读取n
行,其中n<=N
和m<=M
我已经尝试过类似以下内容:
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
但是,当n<N
,我需要跳过当前块中的其余行。 我试过了
if line != '\n':
continue
# do something with the line .....
旁边# do something with the line .....
,但这会跳过整个第一个程序段。
另外,我尝试创建一个迭代器it = lines.iter()
并相应地递增每个迭代器。 这种方法的问题是无法知道何时到达文件末尾。 readlines()
为我完成了此操作,但如果使用中介程序,我不知道如何知道何时到达最后一行。
有什么帮助吗? 谢谢!
您可以像这样测试迭代器的结尾:
try:
it.next()
except StopIteration:
*do something*
StopIteration Exception是在没有任何要迭代的情况下抛出的迭代器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.