How to convert UTF8 string into HTML string in python 2.5 for correct accent displaying?

html, python, utf-8

Solution

First you should make sure `value` is of type unicode and not a string

value.encode('ascii', 'xmlcharrefreplace')

Should get you the HTML enitites

Python Unicode Documentation

>>> value = u"roulement \u00e0 billes"
>>> print value
roulement à billes
>>> print value.encode('ascii', 'xmlcharrefreplace')
roulement à billes
>>>

Problem

My string UFT8, coming from a database (CSV file encoded in UTF8) is displayed like this on a browser with my main.py code: `value ="roulement \u00e0 billes"` => how to convert any of such string into HTML entities, such as value="roulement &agrave billes" in order to display correctly as `roulement à billes` with a browser. I tried to add: ``` # -*- coding: utf-8 -*- ``` on the 1st line of my file , and also : ``` self.response.headers['Content-Type'] = 'text/html;charset=UTF-8' ``` but it doesn't change anything => so, may be another way is to translate it into html entities ? how to ? Thank you.

Original source