[Tutor] compare and arrange file

Peter Otten __peter__ at web.de
Tue Jul 12 11:44:43 CEST 2011


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)




More information about the Tutor mailing list