[Tutor] reading from 2 file output to 1
Evert Rol
evert.rol at gmail.com
Tue Aug 17 10:57:12 CEST 2010
> 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:
This is too much: Looks like you've gathered input from example scripts, and combined them all in the above three lines.
Instead of explaining what's going wrong (sorry; ask again if you really want to know), here's what you could do instead:
while 1:
line = fp1.readline() # read a line and move file pointer to the next line
line2 = ...
<etc>
and probably wrap that in a try-except block to catch the EOF error when there's nothing more to read.
Better is, however,
for line in fp1: # iterating through a file is iterating through its separate lines.
line2 = ...
<etc>
Even better, assuming you're Python >=2.6 (for Python 2.5, use the following at the top of your file: from __future__ import with_statement):
file11 = raw_input('Enter PR1 File name :')
with open(file11) as fp1:
for line in fp1:
line2 = ...
<do stuff>
The with clause, in combination with the open(file1) statement (note: mode 'r' is the default), will neatly close the file afterwards, even if you get an error in the for loop (ie, the with statement takes care of some try-except-finally clauses previously used).
As for whether there are better ways of 'doing it': there are CSV Python modules out there that could help you (I've never used those), and personally I would use a combination of *nix tools like awk & paste for just this. But that's not an option on a Python mailing list ;-).
Hope that helps,
Evert
> 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
More information about the Tutor
mailing list