迭代文件保存块和跳过行

时间:2018-04-23 22:18:51

标签: python python-2.7 iteration

我的数据块是块之间的非数据行。此代码一直在运行但不是健壮。如何在不消耗索引测试中的行的情况下提取块并跳过非数据块?我正在寻找一个没有加载包的直接python解决方案。

我搜索了一个相关的例子,如果答案存在,我很乐意删除这个问题。

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表中使用类似的迭代方法:

for row in ws.iter_rows()

1 个答案:

答案 0 :(得分:1)

在发布的代码中,读取第4行,满足条件index == BLOCK_DATA_ROWS,将第一个循环留给第二个循环。由于fgenerator,当它在第二个循环中被调用时,它返回 next 元素进行迭代,第4行已经返回到循环1(它没有打印,但使用了该值。)

这必须在代码中加以考虑。一种选择是在同一循环中组合两个条件:

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

for i in range(2)也已删除,现在代码可以用于任意数量的块,而不仅仅是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
相关问题