Python — Convert string to HTML/XML entities

by

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.

One Response to “Python — Convert string to HTML/XML entities”

  1. keramida Says:

    That’s not ‘localization aware’ though (try filtering some Greek text through it). I’m afraid I am not a very experienced Python programmer to help writing a version that *is* L17n-aware.

Leave a comment