python - 从txt文件中获取最后10行并排序
问题描述
我从 TXT 文件中得到最后 10 行,如下所示:
a_file = open("log.txt", "r")
lines = a_file.readlines()
last_lines = lines[-10:]
得到这样的答案:
c
d
e
f
g
如何对结果进行排序以使最后一行位于顶部?
g
f
e
d
c
解决方案
使用 reversed() 逐行向后读取文件:
a_file = open("log.txt", "r")
lines = a_file.readlines()
for line in reversed(lines):
print(line)
推荐阅读
- numpy - 如何让我的阵列适合 sklearn?
- windows - 为什么 gcloud auth activate-service-account 在 Windows 上不起作用?
- salesforce - 在 Salesforce Lightning Web 组件中使用 Handsontable 生成的空表
- sulu - Sulu CMS:有没有办法在模板中查看,天气是渲染预览?
- python-3.x - 循环遍历特定文件的目录中的文件。如果文件不存在,则将“找不到文件”附加到列表中,否则附加文件位置
- html - Tizen 中的 HTML5 视频元素非常慢(三星智能电视开发)
- python - 如何使用 Unittest 在 Python 中编写内部类测试方法
- vba - 访问数据库到 Google 地球的超链接地址
- c - C语言错误;分段错误与方法
- php - 如何在 Wordpress 中包含和使用 Laravel 布局?