按第一个元素排序文本文件

sort text file by first element

我生成了一个 txt 文件:

6034;3011;571;;;; 

61443;1571;3111;;;;

6150;2994;242;;;;

6028;26;994;;;;

12054;24;262;3011;571;;

19758.90;0;1;;;;

我想根据行的第一个数字从高到低排列,以便编写另一个 txt 文件。

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

结果:

['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']