0

I am trying to remove comment tags from local .xml files using the BeautifulSoup library.

sample XML

<?xml version="1.0" encoding="UTF-8"?>
<note>
  <!-- remove this -->
 <to>Tove</to>
 <from>Jani</from>
 <heading>Reminder</heading>
 <body>Don't forget me this weekend!</body>
</note>

python

from bs4 import BeautifulSoup as BS
from bs4 import Comment

soup = BS(open("sample.xml"))

comments=soup.find_all(string=lambda text:isinstance(text,Comment))

for c in comments:
    c.decompose()

When I run this code I get the following error:

AttributeError: 'Comment' object has no attribute 'decompose'

I am not sure why I am getting this error.

James Dean
  • 205
  • 4
  • 10
  • 1
    @DavidZemens ... I would highly advise not to follow the dup link as one should not run [regex on X|HTML](https://stackoverflow.com/a/1732454/1422451). My answer to OP would have been to run XSLT using Python's lxml. See [demo fiddle](https://xsltfiddle.liberty-development.net/bFDb2Cn). – Parfait Jul 15 '18 at 15:00
  • 1
    @Parfait see the highest-upvoted answer on the dupe link, which does not use regex – David Zemens Jul 15 '18 at 18:25
  • Solution: Use extract() in place of decompose() – James Dean Jul 16 '18 at 11:21

0 Answers0