Python中的文件排序 [英] File Sorting in Python

查看:49
本文介绍了Python中的文件排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据数值对Python文件进行排序:

I would like to sort a file in Python based on numerical values:

我的输入文件如下:

66135 - A
65117 - B
63301 - C
63793 - D

输出应为:

63301 - C
63793 - D
65117 - B
66135 - A

该怎么做?

推荐答案

f2.writelines(sorted(f1, key=lambda line:int(line.split()[0])))

其中f2是您的输出文件,而f1是您的输入文件.

where f2 is your output file and f1 is your input file.

这篇关于Python中的文件排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆