1

Hi i'm trying to delete a line in my file but want to keep the rest of my lines.

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()

When I use the code above, all my other lines in the file of my html is removed.

text trying to get rid of:

        <h2>Thank you</h2>
<h2>Please come again</h2> #Get rid of this line
Kevin
  • 47
  • 5

2 Answers2

1

Try this one:

with open("myfile.html", "w+") as f:
    content = f.read()
    f.write(re.sub(r'<\s*h2[^>]*>(.*?)<\s*/\s*h2>', '', content))

But as @Willem Van Onsem recommended, don't use regexes for XML/HTML, it's more robust to use XML parser, lxml of BeautifulSoup.

grundic
  • 4,641
  • 3
  • 31
  • 47
0

When you write to same file, the content will be override. So,You need to open new file and write to that file as :

f = open("NEWFILE.html","w")
for line in lines:
  if line!= a[0]:
    f.write(line)
f.close()
Sijan Bhandari
  • 2,941
  • 3
  • 23
  • 36