怎样用python对一个txt文件的所有行做重新排列

2024-11-25 12:05:44
推荐回答(1个)
回答1:

with open(r'foo.txt', 'r') as f:
text = f.read()
lines = sorted(text.splitlines(), cmp=(lambda a, b: ord(a[4])-ord(b[4]) if a[4]!=b[4] else 1 if (a.split()[2]>b.split()[2]) else -1))