I was looking for a function to transform a unicode string into
htmlentities. Not only the usual html escaping thing but all
characters.
As I didn't find I wrote my own:
# -*- coding: utf-8 -*-
from htmlentitydefs import codepoint2name
def unicode2htmlent ities(u):
htmlentities = list()
for c in u:
if ord(c) < 128:
htmlentities.ap pend(c)
else:
htmlentities.ap pend('&%s;' % codepoint2name[ord(c)])
return ''.join(htmlent ities)
print unicode2htmlent ities(u'São Paulo')
Is there a function like that in one of python builtin modules? If not
is there a better way to do it?
Regards, Clodoaldo Pinto Neto
htmlentities. Not only the usual html escaping thing but all
characters.
As I didn't find I wrote my own:
# -*- coding: utf-8 -*-
from htmlentitydefs import codepoint2name
def unicode2htmlent ities(u):
htmlentities = list()
for c in u:
if ord(c) < 128:
htmlentities.ap pend(c)
else:
htmlentities.ap pend('&%s;' % codepoint2name[ord(c)])
return ''.join(htmlent ities)
print unicode2htmlent ities(u'São Paulo')
Is there a function like that in one of python builtin modules? If not
is there a better way to do it?
Regards, Clodoaldo Pinto Neto
Comment