By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,846 Members | 1,862 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,846 IT Pros & Developers. It's quick & easy.

insert comments into elementtree

P: n/a
Hi, I'm using the TidyHTMLTreeBuilder to generate some elementtrees from
html. One by-product is that I'm losing comments embedded in the html. So
I'm trying to put them back in, but I'm doing something wrong: here's the
code snippet of how I generate the Trees:

from elementtree import ElementTree as ET
from elementtidy import TidyHTMLTreeBuilder
XHTML = "{}"

htmfile = os.path.join(self.htmloc,filename)
fd = open(htmfile)
tidyTree = TidyHTMLTreeBuilder.TidyHTMLTreeBuilder('utf-8')
tmp = tidyTree.close()
print 'Bad file: %s\nSkipping.' % filename
tree = ET.ElementTree(tmp)

and here's the method I use to put the comments back in:

def addComments(self,tree):
body = tree.find('./%sbody' % XHTML)
for elem in body:
if elem.tag == '%sdiv' % XHTML and elem.get('class'):
if elem.get('class') == 'remapped':
comElem = ET.SubElement(elem,ET.Comment('stopindex'))

filename = os.path.join(self.deliverloc,name)
self.htmlcontent.write(tree,filename,encoding=self .encoding

when I try this I get errors from the ElementTree _write method:
TypeError: cannot concatenate 'str' and 'instance' objects

thanks for any help!
--Tim Arnold

Nov 16 '07 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.