[Tutor] reading from 2 file output to 1
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 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@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reading from 2 file output to 1
> 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 = ... 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 = ... 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 = ... 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 >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@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reading from 2 file output to 1
"nitin chandra" wrote in message news:aanlktimjrqfh=tdqtric7=utfgbxjcgaaai9coynn...@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/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reading from 2 file output to 1
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 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 >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@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reading from 2 file output to 1
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 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 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 >>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@python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] box drawing characters
On 8/17/2010 12:30 AM, Bill Allen wrote: I am looking for some guidance about how to best utilize box drawing characters(using unicode?) in as portable a way as possible in Python. Any good guides out there for this? Please tell us a bit more about your goals. -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] box drawing characters
On Tue, Aug 17, 2010 at 10:00 AM, bob gailer wrote: > On 8/17/2010 12:30 AM, Bill Allen wrote: > >> I am looking for some guidance about how to best utilize box drawing >> characters(using unicode?) in as portable a way as possible in Python. Any >> good guides out there for this? >> > > Please tell us a bit more about your goals. > > > I want to be able to create some user interfaces, similar to what you see in console based programs like Midnight Commander or other TUI type programs that use box drawing characters to create a user interfaces for input and display of information. However, I do want them to be portable to most platforms that Python will run on. I have actually found much more information on designing fully graphical GUI interfaces using Tk or Qt or GTK, but I know the other type is possible. I suppose I have a preference for the older console type programs. Bill ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Error Using exec to Run Module Files
I'm a rank beginner in Python. Took advantage of the O'Reilly one-day sale of 'Learning Python' announced on the tutor, and starting to work my way through it. When I tried out an example from pg57, on Using exec to Run Module Files, I get the following result: >>> exec(open('script1.py').read()) Traceback (most recent call last): File "", line 1, in File "", line 1 %!PS-Adobe-3.0 ^ SyntaxError: invalid syntax What is going on/ Gene R. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Error Using exec to Run Module Files
Am 18.08.2010 06:54, schrieb mes...@juno.com: I'm a rank beginner in Python. Took advantage of the O'Reilly one-day sale of 'Learning Python' announced on the tutor, and starting to work my way through it. When I tried out an example from pg57, on Using exec to Run Module Files, I get the following result: exec(open('script1.py').read()) Traceback (most recent call last): File "", line 1, in File "", line 1 %!PS-Adobe-3.0 ^ SyntaxError: invalid syntax It looks like the first line of your file "script1.py" contains "%!PS-Adobe-3.0". (Which is no valid syntax, of course) Have you doublechecked the content of script1.py and does it work by invoking it form the command line with "python script1.py"? My guess is the file somehow got screwed up. HTH Jan What is going on/ Gene R. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor