Kevin
Kevin

Reputation: 47

Deleting a specific line of a file

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

Upvotes: 1

Views: 66

Answers (2)

grundic
grundic

Reputation: 4921

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.

Upvotes: 1

Sijan Bhandari
Sijan Bhandari

Reputation: 3051

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

Upvotes: 0

Related Questions