Ola Natvig wrote:

> Does anyone know a good library for transfering non standard characters 
> to enity characters in html. I want characters like < and > to be 
> transformed to &lt; and &gt;. And the norwegian ø to &oslash;
> 

You could use cgi.escape to handle &, <, and > and then use error handling 
on unicode.encode to handle the other characters. That doesn't do quite 
what you ask since your ø will become &#248:

>>> s = u'<ø>'
>>> cgi.escape(s).encode('ascii', 'xmlcharrefreplace')
'&lt;&#248;&gt;'


If you really want named entities, then have a look at 
lib/test/test_codeccallbacks which has a test called 
test_xmlcharnamereplace that registers another codec error handler 
'test.xmlcharnamereplace'. I think you could probably extract that and use 
it as above.
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to