Unicode to HTML entities

Duncan Booth duncan.booth at invalid.invalid
Wed May 30 03:25:41 EDT 2007


Clodoaldo <clodoaldo.pinto at gmail.com> wrote:

> On May 29, 12:57 pm, "Richard Brodie" <R.Bro... at rl.ac.uk> wrote:
>> "Clodoaldo" <clodoaldo.pi... at gmail.com> wrote in message
>>
>> news:1180453921.357081.89500 at n15g2000prd.googlegroups.com...
>>
>> >I was looking for a function to transform a unicode string into
>> >htmlentities.
>> >>> u'São Paulo'.encode('ascii', 'xmlcharrefreplace')
>>
>> 'São Paulo'
> 
> That was a fast answer. I would never find that myself.
> 
You might actually want:

>>> cgi.escape(u'São Paulo & Espírito Santo').encode('ascii', 'xmlcharrefreplace')
'São Paulo & Espírito Santo'

as you have to be sure to escape any ampersands in your unicode 
string before doing the encode.




More information about the Python-list mailing list