Created
May 31, 2011 00:04
-
-
Save gnrfan/999656 to your computer and use it in GitHub Desktop.
Python snippet: unicode2htmlentities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from htmlentitydefs import codepoint2name | |
def unicode2htmlentities(u): | |
htmlentities = list() | |
for c in u: | |
if ord(c) < 128: | |
htmlentities.append(c) | |
else: | |
htmlentities.append('&%s;' % codepoint2name[ord(c)]) | |
return ''.join(htmlentities) | |
print unicode2htmlentities(u'Juan Pérez') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment