迭代文件保存块和跳过行 [英] Iterate File Saving Blocks and Skipping Lines
问题描述
我在块之间有非数据线的块数据。此代码一直在运行,但不是健壮。如何在不消耗索引测试中的行的情况下提取块并跳过非数据块?我正在寻找一个没有加载包的直接python解决方案。
I have data in blocks with non-data lines between the blocks. This code has been working but is not robust. How do I extract blocks and skip non-data blocks without consuming a line in the index test? I'm looking for a straight python solution without loading packages.
我搜索了一个相关的例子,如果答案存在,我很乐意删除这个问题。
I've searched for a relevant example and I'm happy to delete this question if the answer exists.
from __future__ import print_function
BLOCK_DATA_ROWS = 3
SKIP_ROWS = 2
block = 0
with open('array1.dat', 'rb') as f:
for i in range (2):
block += 1
for index, line in enumerate(f):
if index == BLOCK_DATA_ROWS:
break
print(block, 'index', index, 'line', line.rstrip('\r\n'))
for index, line in enumerate(f):
if index == SKIP_ROWS:
break
print(' skip index', index, 'line', line.rstrip('\r\n'))
输入
1
2
3
4
5
6
7
8
9
输出
1 index 0 line 1
1 index 1 line 2
1 index 2 line 3
skip index 0 line 5
skip index 1 line 6
2 index 0 line 8
2 index 1 line 9
修改
我还想在excel表中使用类似的迭代方法:
I also want to use a similar iteration approach with an excel sheet:
for row in ws.iter_rows()
推荐答案
在发布的代码中,读取第4行,满足条件 index == BLOCK_DATA_ROWS
,将第一个循环留给第二个循环。由于 f
是生成器,它在第二个循环中调用,它返回 next 元素进行迭代,第4行已经返回到循环1(它没有打印,但是使用了该值)。
In the code posted, the line 4 is read, and the condition index == BLOCK_DATA_ROWS
is met, leaving the first loop towards the second one. As f
is a generator, when it is called in the second loop, it returns the next element to iterate over, and line 4 has already been returned to loop 1 (it is not printed, but the value is used).
这必须在代码中加以考虑。一种选择是在同一循环中组合两个条件:
This has to be taken into account in the code. One option is to combine both conditions in the same loop:
from __future__ import print_function
BLOCK_DATA_ROWS = 3
SKIP_ROWS = 2
block = 1
with open('array1.dat', 'r') as f:
index = 0
for line in f:
if index < BLOCK_DATA_ROWS:
print(block, 'index', index, 'line', line.rstrip('\r\n'))
elif index < BLOCK_DATA_ROWS+SKIP_ROWS:
print(' skip index', index, 'line', line.rstrip('\r\n'))
index += 1
if index == BLOCK_DATA_ROWS+SKIP_ROWS: # IF!!, not elif
index = 0
block += 1
范围(2)中的i的也已被删除,现在代码适用于任意数量的块,而不仅仅是2。
The for i in range(2)
has also been removed, and now the code will work for any number of blocks, not just 2.
返回:
1 index 0 line 1
1 index 1 line 2
1 index 2 line 3
skip index 3 line 4
skip index 4 line 5
2 index 0 line 6
2 index 1 line 7
2 index 2 line 8
skip index 3 line 9
skip index 4 line 10
这篇关于迭代文件保存块和跳过行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!