elementtree and inclusion of special characters

  • Thread starter =?iso-8859-1?B?QW5kcuk=?=
  • Start date
?

=?iso-8859-1?B?QW5kcuk=?=

I'm using elementtree to process some html files, by building a tree,
manipulating it, and writing it back. One problem I encounter is that
elementtree converts some symbols in an unwanted way. For example, the
symbol ">" is converted to ">". This is fine in html code, but not
if the page includes some script like the example below

<script type="text/javascript">
function init() {
var a = 1;
if (a > 0)
{
alert("Spam alert");
}
}
</script>

The resulting code, with "a &gt; 0" is not understood by the browser...

Any suggestions as to how I can circumvent this problem in an easy way?

André
 
F

Fredrik Lundh

André said:
I'm using elementtree to process some html files, by building a tree,
manipulating it, and writing it back. One problem I encounter is that
elementtree converts some symbols in an unwanted way. For example, the
symbol ">" is converted to "&gt;". This is fine in html code, but not
if the page includes some script like the example below

<script type="text/javascript">
function init() {
var a = 1;
if (a > 0)
{
alert("Spam alert");
}
}
</script>

The resulting code, with "a &gt; 0" is not understood by the browser...

ET is an XML library, and an XHTML-aware browser has no problems dealing
with that, but I assume you might want to support tag soup parsers like
IE6 as well ;-)

to write true HTML 4.0, you need a HTML serializer. there's a good one
in Kid (though I don't know how hard it would be to use that one with a
preexisting tree, rather than a Kid "event stream").

another alternative is the HTMLTree class in Ian Bicking's commentary
application:

http://svn.pythonpaste.org/Paste/apps/Commentary/trunk/commentary/dumbpath.py

(you may have to tweak that module somewhat to be able to use it without
elementtidy).

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top