Reading data from 2 different files and writing to a single file

Virgil Stokes vs at it.uu.se
Mon Jan 28 12:29:03 EST 2013


On 28-Jan-2013 15:49, Chris Angelico wrote:
> On Tue, Jan 29, 2013 at 1:37 AM, Dave Angel <d at davea.name> wrote:
>> What you want is the zip() function
>>
>> for l,s in zip(f1, f2):
>>      #you now have one line from each file,
>>      #   which you can then validate and process
>>
>> Note, this assumes that when a line is "bad" from either file, you're going
>> to also ignore the corresponding line from the other.  If you have to
>> accommodate variable misses in the lining up, then your work is *much*
>> harder.
> Much harder? Not really - see my solution above with a simple 'break'.
> Much less clear what's going on, though, it is. Iterating together
> over both files with zip is much cleaner.
>
> ChrisA
Nice example of the power of zip, Chris :-)



More information about the Python-list mailing list