Archive for October, 2008

Python — Convert string to HTML/XML entities

October 18, 2008

In order to convert a string to HTML/XML entities you can use:

”.join(['&#%d;' % ord(ch) for ch in text])

For example if text=’mailto’ the output would be:

‘& #109;& #97;& #105;& #108;& #116;& #111;’

This might be useful in hiding email addresses in web pages.