python - 将文件的内容从下到上翻转,第一行除外
问题描述
我正在尝试将除第一行之外的行颠倒过来:例如:
Header
first
second
third
带有以下代码的输出是:
third
second
first
Header
我想做的是:
Header
third
second
first
代码如下:
with open('file.txt', 'r') as f:
lines = f.readlines()
with open('output.txt', 'w+') as f:
for l in reversed(lines):
f.write(l)
解决方案
with open('file.txt', 'r') as f:
lines = f.readlines()
with open('output.txt', 'w+') as f:
f.write(l[0])
for l in reversed(lines[1:]):
f.write(l)
推荐阅读
- react-native - 带有 react native 和 APK 的 MirageJS
- bash - 如何将非交互式参数传递到使用“读取”的 bash 文件中?
- python - Selenium WebDriverException 当 self.driver_.get("http://www.stockbroking.com.my")
- typescript - 我正在寻找一个`PickOptional
` 通用 - amazon-web-services - SNS DwellTimeMs 闲置一段时间后为高
- python - 您如何自动滚动 Instagram DM?
- c# - C#比较存储在XML文件中的两个日期时间
- node.js - NodeJs:批量插入到 SQL Server 一对多
- html - 点击时的css动画播放状态
- amazon-web-services - 用户登录 AWS Cognito 后如何解析 JWT 令牌