Skip to content

Instantly share code, notes, and snippets.

@gnrfan
Created May 31, 2011 00:04
Show Gist options
  • Save gnrfan/999656 to your computer and use it in GitHub Desktop.
Save gnrfan/999656 to your computer and use it in GitHub Desktop.
Python snippet: unicode2htmlentities
# -*- 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