【发布时间】:2018-01-22 09:59:15
【问题描述】:
我确定这是一个简单的 readlines 解决方案。我花了一些时间试图解决它但没有成功。
我有一段文字如下:
"This is the text 1"
0.0 2.0 2.0 2.0
0.0 0.0 4.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
"This is the text 2"
0.0 5.0 3.0 5.0
0.0 0.0 6.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
我正在努力将其更改为:
"This is the text 1"
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 4.0 0.0
0.0 2.0 2.0 2.0
"This is the text 2"
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 6.0 0.0
0.0 5.0 3.0 5.0
非常感谢任何帮助。 谢谢
代码:
f = open("file.txt", "rb")
s = f.readlines()
f.close()
f = open("newtext.txt", "wb")
f.writelines(s[::-1])
f.close()
返回整个 txt 文件倒置。
【问题讨论】:
-
您需要向我们展示您目前编写的代码类型以及您遇到的问题。
-
欢迎来到 Stack Overflow,请花点时间通过the welcome tour 了解您在这里的道路(同时也获得您的第一个徽章),阅读如何create a Minimal, Complete, and Verifiable example 并查看@987654323 @ 这样您就可以增加获得反馈和有用答案的机会。
-
@Chris 道歉,我当前的方法返回整个文件倒置,我需要将标题“这是文本 1”保留在同一个地方
-
@kazemakase 对格式表示抱歉,谢谢
-
@Qwerty 无需道歉。只要确保我做对了;)