user3351370
user3351370

Reputation: 335

sort text file by first element

I generate a txt file :

6034;3011;571;;;; 

61443;1571;3111;;;;

6150;2994;242;;;;

6028;26;994;;;;

12054;24;262;3011;571;;

19758.90;0;1;;;;

I would like to rank it from highest to lowest according to the first number of the line in order to write another txt file.

Upvotes: 0

Views: 321

Answers (1)

Termi
Termi

Reputation: 661

with open("sample.txt") as f:
    lines = f.readlines()
    print sorted(lines, key=lambda x:float(x.split(';')[0]), reverse=True)

result:

['61443;1571;3111;;;;\n', '19758.90;0;1;;;;\n', '12054;24;262;3011;571;;\n', '6150;2994;242;;;;\n', '6034;3011;571;;;; \n', '6028;26;994;;;;\n']

Upvotes: 1

Related Questions