Dnaiel
Dnaiel

Reputation: 7832

return lines of multiple files iteratively

Let us say I have a dictionary containing the file names and fullnames of 3 text files, all of the same number of lines, N.

dof = {'myf1':'/a/b/myf1.txt', 'myf2':'/a/b/myf2.txt', 'myf3':'/a/b/myf3.txt'}

What'd be the most efficient/pythonic way to iterate over all files, each line as follows:

line 1 of file 1, then line 1 of file 2, then line 1 of file 3;
line 2 of file 1, then line 2 of file 2, then line 2 of file 3;
...
line N of file 1, then line N of file 2, then line N of file 3; 

A general solution for any number of files in the dictionary and any number of total lines would be preferable.

Upvotes: 1

Views: 72

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122412

Use zip() to iterate over the lines in triples:

for line1, line2, line3 in zip(openfile1, openfile2, openfile3):

You could produce the open file handles with:

files = [open(dof[name]) for name in sorted(dof)]

for lines in zip(*files):

where lines is a tuple of strings, the next line of each of the input files.

If the input files do not have the same number of lines, you can use itertools.izip_longest() instead to provide a replacement value for those lines missing (use itertools.zip_longest() in Python 3).

Upvotes: 4

Related Questions