[Tutor] reading from 2 file output to 1

Alan Gauld alan.gauld at btinternet.com
Tue Aug 17 11:14:54 CEST 2010


"nitin chandra" <nitinchandra1 at gmail.com> wrote in message 
news:AANLkTimjRqFH=TdQtRic7=utFgbXjCGaaai9coYNNdJD at mail.gmail.com...
> Hello All,
>
> I am trying to read from 2 CSV files, where first 4 ([0,1,2,3])
> columns are read from 'file1' and 3 columns ([1,2,3]) from 'file2' 
> and
> write them into a 3rd file 'file3', 7 columns string. The data is
> Numeric values both, +ve and -ve.

The data types should be irrelevant since it is all strings in the 
file.
But reading your requirement you want something like:

while nTrue
  try:
    line1A = file1.readline()
    line1B = file2.readline()
    file3.write(line1A + line1B)
  except IOError:
      reached end of one of the files,
      figure out how to handle it...

That looks like it should be simpler than the code you posted...


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list