[Tutor] reading from 2 file output to 1

nitin chandra nitinchandra1 at gmail.com
Tue Aug 17 10:39:11 CEST 2010


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.

this is how i was trying.

******************************************
import sys,os, fileinput


file11 = raw_input('Enter PR1 File name :')
fp1 = open(file11,'r')

file12 = raw_input('Enter PR3 File Name :')
fp2 = open(file12,'r')


while 1:
    fp1 = fp1.readline()
      for line in fp1:
         line2 = line.split(",")
         col1 = line2[0]
         col2 = line2[1]
         col3 = line2[2]
         col4 = line2[3]
         print col1
         print col2
         FL1 = '%s,%s,%s,%s' % (col1,col2,col3,col4)
         print FL1

while 1:
    fp2 = fp2.readline()
       for line1 in fp2:
          line3 = line1.split(",")
          col5 = line3[1]
          col6 = line3[2]
          col7 = line3[3]

          FL2 = '%s,%s,%s,' % (col5,col6,col7)
          print FL2

file3=raw_input('Enter PR2 OUTPUT File Name :')
fp3 = open(file3,'w')

print col1,col2,col3,col4 + ',' + col5,col6,col7

str3 = '%s,%s,%s,%s,%s,%s,%s\n' % (col1,col2,col3,col4,col5,col6,col7)
fp3.write(str3)

**********************************************

I am getting the following error :

Traceback (most recent call last):
  File "mer5Pr2.py", line 16, in <module>
    col2 = line2[1]
IndexError: list index out of range

*********************************************************

There is data in the file at 'col2'. So what am i missing / doing wrong ?

And is there better / optimized way of doing it?

Thanks

Nitin


More information about the Tutor mailing list