471,325 Members | 1,701 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

insert comments into elementtree

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 = "{http://www.w3.org/1999/xhtml}"

htmfile = os.path.join(self.htmloc,filename)
fd = open(htmfile)
tidyTree = TidyHTMLTreeBuilder.TidyHTMLTreeBuilder('utf-8')
tidyTree.feed(fd.read())
fd.close()
try:
tmp = tidyTree.close()
except:
print 'Bad file: %s\nSkipping.' % filename
continue
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'))

self.addComments(tree)
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
0 968

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Stewart Midwinter | last post: by
1 post views Thread by Greg Wilson | last post: by
reply views Thread by Björn Lindström | last post: by
15 posts views Thread by Steven Bethard | last post: by
reply views Thread by Greg Aumann | last post: by
2 posts views Thread by mirandacascade | last post: by
2 posts views Thread by mirandacascade | last post: by
5 posts views Thread by saif.shakeel | last post: by
1 post views Thread by Mike Slinn | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.