从每个文件一次交错多个文件,连续两行或多行

时间:2016-07-09 00:21:20

标签: python

我已搜索并找到下面的代码,可以将2个文件逐行交错为新文件。

from itertools import izip_longest
from contextlib import nested

with nested(open('foo'), open('bar')) as (foo, bar):
    for line in (line for pair in izip_longest(foo, bar)
                      for line in pair if line):
        print line.strip()

我有多个文件,喜欢连续两行或多行交错。我希望能够根据工作选择行数。每个文件中的总行数可能不同,但所有文件中每个元素的行数模式始终相同。我怎样才能实现目标?

input:
fileA
lineA1
lineA2
lineA3
......

fileB
lineB1
lineB2
lineB3
......

For 2 lines output:
lineA1
lineA2
lineB1
lineB2
.....

For 3 lines output:
lineA1
lineA2
lineA3
lineB1
lineB2
lineB3
....

谢谢。

@xealits万分感谢。你的代码就像冠军一样。祝你有愉快的一天!

1 个答案:

答案 0 :(得分:1)

这应该有效:

from itertools import islice

# number of sequential lines to read from each file
N = 2
# files that are read
files = [open(n) for n in ['foo', 'bar', 'baz']]

line = ''.join([''.join(islice(f, N)) for f in files])[:-1]
while line:
    print(line)
    line = ''.join([''.join(islice(f, N)) for f in files])[:-1]

[f.close() for f in files]

- 此处文件在列表推导中手动打开和关闭,而不是使用withnestedExitStack;当调用文件islice从它读取前N行时(当读取行时,它们不再在文件对象中 - 它们会弹出它)(同样,可以循环遍历文件[line for line in file]); ''.join将给定list / tuple / iterable对象的项连接到以''为分隔符的字符串中;由于print为打印的字符串添加换行符,[:-1]将从文件中删除结果字符串的最后一个字符,即换行符。

如果您只阅读了2个文件,那么with就可以了:

from itertools import islice

# number of sequential lines to read from each file
N = 2

with open('foo') as foo, open('bar') as bar:
    line = ''.join(islice(foo, N)) + ''.join(islice(bar, N))[:-1]
    while line:
        print(line)
        line = ''.join(islice(foo, N)) + ''.join(islice(bar, N))[:-1]

使用过的东西的一些参考:

How to read file N lines at a time in Python?

nested is deprecated

Python 3 uses ExitStack for nested features in with

ExitStack