tags:

views:

67

answers:

3

I have two files that are zipped with something like the following:

for line in zip(open(file1), open(file2)):
    # do-something

Unfortunately, now file2 has changed, and there is an additional line at the beginning. Yes, I could get rid of that manually (or with an additional script/program), but since the actual number of involved files is huge, I'd prefer to solve the problem at this level.

So, what I want is something like the following (which would be valid, if open(file) were subscriptable):

for line in zip(open(file1), open(file2)[1:]):
    # do-something
+2  A: 

Take a look at itertools:

for line in itertools.izip(
    open(file1),
    itertools.islice(open(file2), 1, None)
    ):
    # do something

Edit: changed from zip to the itertools.izip function.

gahooa
See the edit above.
gahooa
I'd also recommend switching to `itertools.izip`, as Alex suggests, to save memory.
Ben Blank
+1  A: 
f1 = open(file1)
f2 = open(file2)
f2.next()   # Skip the first line of file2 (used to be readline() - thanks, Alex)
for line in zip(f1, f2):
    # do-something
RichieHindle
Risky -- it works in the current CPython implementation, but generally mixing iteration on files and calls to other file methods is fragile and not guaranteed, see http://docs.python.org/library/stdtypes.html?highlight=readline#file.next (in the current CPython implementation, `next` then `readline` breaks, vice versa generally works as of today -- but, why risk it when `next(f2)` is a perfectly solid and guaranteed way to "skip the first line of file2"?!).
Alex Martelli
Fixed - thanks!
RichieHindle
+4  A: 

open gives you an iterator, so it's not "subscriptable" but it can easily be advanced by one (with the next builtin in 2.6 or better, the .next() method in older Python versions -- I'm assuming 2.6 or better here).

So where you'd like to say:

for line in zip(open(file1), open(file2)[1:]):

say, instead:

f2 = open(file2)
next(f2)
for line in zip(open(file1), f2):

or, if you're keen on one-liners:

import itertools as it
for line in it.izip(open(file1), it.islice(open(f2), 1, None)):

In the latter case, since I'm importing itertools anyway, I also take advantage to use its izip approach rather than the memory-wasting zip built-in;-).

Alex Martelli
+1 Actually I am using izip :-) but wanted to keep the question simpler. Thanks!
Davide