Re: [Tutor] compare and arrange file
On 7/12/2011 4:01 PM Edgar Almonte said... On Tue, Jul 12, 2011 at 5:44 AM, Peter Otten<__pete...@web.de> wrote: import csv imports the comma separated values (csv) file handler utilities module def sortkey(row): if float(row[1]): return row[1], True else: return row[2], False ... sortkey defines a function that accepts a cvs.reader data row, and returns either row[1] and True or row[2] and False based on which of row[1] and row[2] has a non-zero value with open("infile.txt", "rb") as instream: rows = sorted(csv.reader(instream, delimiter="|"), key=sortkey) rows becomes the sortkey sorted result of the lines of infile.txt with open("outfile.txt", "wb") as outstream: csv.writer(outstream, delimiter="|").writerows(rows) ... and this writes those results to outfile.txt you might also try the shortened: from csv import reader,writer def sortkey(row): return max(row[1],row[2]),row[1]>row[2] writer(open("outfile.txt", "wb"), delimiter="|").writerows( sorted(reader(open("infile.txt", "rb"), delimiter="|"),key=sortkey)) Emile ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compare and arrange file
On Tue, Jul 12, 2011 at 5:44 AM, Peter Otten <__pete...@web.de> wrote: > Edgar Almonte wrote: > >> thanks emile i understand exactly what you explain me but i was unable >> to accomplish it ( too noob in python ) but i solved the problem with >> this code >> http://pastebin.com/4A6Jz4wZ >> >> i will try do what you suggest me anyway but that will tomorrow , >> tonight i done and i feel good :D > > When you're done compare it to the one below: > > > import csv > > def sortkey(row): > if float(row[1]): > return row[1], True > else: > return row[2], False > > with open("infile.txt", "rb") as instream: > rows = sorted(csv.reader(instream, delimiter="|"), key=sortkey) > > with open("outfile.txt", "wb") as outstream: > csv.writer(outstream, delimiter="|").writerows(rows) > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > That code work flawless , aweasome , can you explain to me the code, i have a idea but if you don't mind can you ? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compare and arrange file
On 07/12/2011 08:25 AM, Edgar Almonte wrote: On Tue, Jul 12, 2011 at 7:28 AM, Dave Angel wrote: On 07/12/2011 12:56 AM, Edgar Almonte wrote: thanks emile i understand exactly what you explain me but i was unable to accomplish it ( too noob in python ) but i solved the problem with this code http://pastebin.com/4A6Jz4wZ (When you post on this list, your comments should follow the pieces you're responding to. That's a long-standing convention.) As I explained earlier, your nested loops won't work correctly, as you fail to re-open the orig1 file. Since you still seem to think it'll work, I'll just hope this assignment is for fun, and not for anything that matters. In particular, the first line will be compared to all the others. But if the third line should have matched the seventh, it won't get written out. -- DaveA hmm i still don't get you point , i mean why you say that will fail , i copy/read the file 2 times and compare line by line with the second copy of the file ( check the new code at top i do a read() for full read it not more online reading ). anyway i will do the case that you explain and look the result. You're absolutely right. I missed the fact that you did a read() and split() at the top. As long as you have enough memory to hold two copies of the file, that's great. DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compare and arrange file
On Tue, Jul 12, 2011 at 7:28 AM, Dave Angel wrote: > On 07/12/2011 12:56 AM, Edgar Almonte wrote: >> >> thanks emile i understand exactly what you explain me but i was unable >> to accomplish it ( too noob in python ) but i solved the problem with >> this code >> http://pastebin.com/4A6Jz4wZ >> > (When you post on this list, your comments should follow the pieces you're > responding to. That's a long-standing convention.) > > As I explained earlier, your nested loops won't work correctly, as you fail > to re-open the orig1 file. Since you still seem to think it'll work, I'll > just hope this assignment is for fun, and not for anything that matters. > > In particular, the first line will be compared to all the others. But if > the third line should have matched the seventh, it won't get written out. > -- > > DaveA > > hmm i still don't get you point , i mean why you say that will fail , i copy/read the file 2 times and compare line by line with the second copy of the file ( check the new code at top i do a read() for full read it not more online reading ). anyway i will do the case that you explain and look the result. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compare and arrange file
On Tue, Jul 12, 2011 at 5:44 AM, Peter Otten <__pete...@web.de> wrote: > Edgar Almonte wrote: > >> thanks emile i understand exactly what you explain me but i was unable >> to accomplish it ( too noob in python ) but i solved the problem with >> this code >> http://pastebin.com/4A6Jz4wZ >> >> i will try do what you suggest me anyway but that will tomorrow , >> tonight i done and i feel good :D > > When you're done compare it to the one below: > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > import csv > > def sortkey(row): > if float(row[1]): > return row[1], True > else: > return row[2], False > > with open("infile.txt", "rb") as instream: > rows = sorted(csv.reader(instream, delimiter="|"), key=sortkey) > > with open("outfile.txt", "wb") as outstream: > csv.writer(outstream, delimiter="|").writerows(rows) > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > This look aweasome i will try it later , thanks ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compare and arrange file
On 07/12/2011 12:56 AM, Edgar Almonte wrote: thanks emile i understand exactly what you explain me but i was unable to accomplish it ( too noob in python ) but i solved the problem with this code http://pastebin.com/4A6Jz4wZ (When you post on this list, your comments should follow the pieces you're responding to. That's a long-standing convention.) As I explained earlier, your nested loops won't work correctly, as you fail to re-open the orig1 file. Since you still seem to think it'll work, I'll just hope this assignment is for fun, and not for anything that matters. In particular, the first line will be compared to all the others. But if the third line should have matched the seventh, it won't get written out. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compare and arrange file
Edgar Almonte wrote: > thanks emile i understand exactly what you explain me but i was unable > to accomplish it ( too noob in python ) but i solved the problem with > this code > http://pastebin.com/4A6Jz4wZ > > i will try do what you suggest me anyway but that will tomorrow , > tonight i done and i feel good :D When you're done compare it to the one below: import csv def sortkey(row): if float(row[1]): return row[1], True else: return row[2], False with open("infile.txt", "rb") as instream: rows = sorted(csv.reader(instream, delimiter="|"), key=sortkey) with open("outfile.txt", "wb") as outstream: csv.writer(outstream, delimiter="|").writerows(rows) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor