1
嗨,我试图删除我的文件中的一行,但想保留我的其余行。删除文件的特定行
f = open("myfile.html").read()
lines = f.readlines()
a = findall('<h2>\$.*', f)
f.close()
f = open("myfile.html","w")
for line in lines:
if line!= a[0]:
f.write(line)
f.close()
当我使用上面的代码时,我的html文件中的所有其他行都被删除。当你写同一个文件,内容将覆盖
<h2>Thank you</h2>
<h2>Please come again</h2> #Get rid of this line
不要用正则表达式处理HTML/XML ... –
@WillemVanOnsem,[no fear](https://gist.github.com/mgsisk/1094230)! =) – grundic
@grundic:我的观点正是...... –