【问题标题】:Iterating through rows in .txt and looping each "15 rows"遍历 .txt 中的行并循环每个“15 行”
【发布时间】:2016-07-03 17:25:14
【问题描述】:

我的 python 脚本有问题。我找不到只计算前 15 行的方法,然后只计算第二个 15 行,然后只计算第三个 15 行……这些行来自一个 txt 文件。

with open('/Users/sammtt/data/test2.txt','r') as f:
for line in nonblank_lines(f):
    print(my_txt(line,0))
    print(my_txt(line,2))

    print(my_txt(line,6))
    print(my_txt(line,8))

非常感谢

【问题讨论】:

  • 只需将所有行附加到列表中,然后进行计算。
  • 是的,但是如何每 15 行跳过一次,有:第一个列表有前 15 行,第二个列表有 15 行等...
  • 使用enumerate怎么样?如果 rowNum % 15 == 0 那么接下来的 15 行。

标签: python list stream rows


【解决方案1】:

您可以将这个recipe 用于标准库中的itertools

from itertools import zip_longest

def grouper(iterable, n, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return zip_longest(*args, fillvalue=fillvalue)

将其应用于您的代码:

with open('/Users/sammtt/data/test2.txt','r') as f:
    for chunk in grouper(nonblank_lines(f), 15):
        process_chunk(chunk)

【讨论】:

  • 非常感谢。我有一个错误说 NameError: name 'zip_longest' is not defined
  • @sammtt: zip_longest 需要从itertools 导入(查看更新)。
【解决方案2】:

您可以在整数除法上使用itertools.groupby

>>> from itertools import groupby
>>> f = range(0, 100)
>>> for i, g in groupby(f, key=lambda x: x//15):
...     print(list(g))
...
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
[15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44]
[45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59]
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74]
[75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89]
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]

对于您的文件对象,您可以使用enumerate 来逐行遍历:

with open('/Users/sammtt/data/test2.txt','r') as f:
    for i, group_of_15 in groupby(enumerate(nonblank_lines(f)), key=lambda x: x[0]//15):
        chunk = list(map(lambda x: x[1], group_of_15))
        do_something(chunk)

【讨论】:

  • 谢谢你。但是,我真的不明白它是如何工作的。一块是 15 行的盒子吗?如何获得块的第一行?例如通过设置 nonblank_lines[0]?
  • 是的,15 行/行。 chunk[0] 应该让你获得第一行。
  • 我为 i, group_of_15 in groupby(enumerate(nonblank_lines(f)), key=lambda x: x[0]//15): chunk = map(lambda x: x[1] , group_of_15) print(chunk[0]) 但我得到 TypeError: 'map' object is not subscriptable
  • @sammtt 啊,您应该在问题中添加了 python 3 标签。我以为这是 Python 2。我已经更新了答案。
  • 我正在做 chunk[0] 以获取例如前 15 行,但它实际上获取每 15 行的第一行。我怎样才能得到第一个块(例如前 15 行)?
猜你喜欢
  • 1970-01-01
  • 2014-06-29
  • 2011-03-09
  • 2019-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-14
相关资源
最近更新 更多