从文本文件中删除一行 [英] Deleting a line from a text file
本文介绍了从文本文件中删除一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用readlines()
喜欢:
f_open = open("textfile.txt", "r")
lines = f_open.readlines()
如何使用lines
在textfile.txt
中选择一行并将其删除?
How do you use lines
to choose a line in textfile.txt
and delete it?
对不起,如果这没有道理.
Sorry if it doesn't make sense.
推荐答案
使用fileinput模块的就地功能.请在fileinput上参考可选的就地过滤部分.下面的示例从文件中删除第一行:
Use the fileinput module's inplace functionality. Refer Optional in-place filtering section at fileinput. The example below deletes the first line from a file:
import fileinput
import sys
for line_number, line in enumerate(fileinput.input('myFile', inplace=1)):
if line_number == 0:
continue
else:
sys.stdout.write(line)
这篇关于从文本文件中删除一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文