[Tutor] reading from 2 file output to 1

Joel Goldstick joel.goldstick at gmail.com
Tue Aug 17 15:31:50 CEST 2010


I haven't worked through the reading of the files, but once you  have the
matching lines from each you can process like this:

def split_and_combine(l1, l2):
  first = l1.split(', ')
  second = l2.split(', ')
  combined = first[:4] + second[1:4]
  return combined

Then write the value to the output file.

-- 
Joel Goldstick


On Tue, Aug 17, 2010 at 6:29 AM, Nitin Das <nitin.162 at gmail.com> wrote:

> I think in both the while loops , for loops are iterating over the new
> lines, as a result col1,col2,col3,col4,col5,col6,col7 as a result after both
> the while loop finishes these colums would only be having the values of the
> last lines from both the files. as a result the new file3 would only be
> having last lines of the both the files appended together, because u r
> combining the columns after both the while loops.
>
> --nitin
>
>
> On Tue, Aug 17, 2010 at 2:09 PM, nitin chandra <nitinchandra1 at gmail.com>wrote:
>
>> 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
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100817/30088169/attachment.html>


More information about the Tutor mailing list