Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
还有一个关于删除前三行和最后一行的问题,他们使用了.writelines(),但只有在你想删除外部行时才有效,而不是删除中间的行。
.writelines()
从python中的文本文件中的任何位置删除多个特定行的任何方法?
请,请,请帮助!
试试下面的代码:
with open('input.txt') as f, open('output.txt', 'w') as fout: fout.writelines(line for lineno, line in enumerate(f, 1) if lineno not in (3,4,5))