Skip to content

Instantly share code, notes, and snippets.

@marcoslhc
Created September 27, 2012 22:46
Show Gist options
  • Save marcoslhc/3796906 to your computer and use it in GitHub Desktop.
Save marcoslhc/3796906 to your computer and use it in GitHub Desktop.
generate html entity codes
import re
from htmlentitydefs import codepoint2name
# convert unicode or string to raw
def raw(s):
# raw function thanks to stackoverflow.com/users/1438542/balbc
if isinstance(s, str):
s = s.encode('string-escape')
elif isinstance(s, unicode):
s = s.encode('unicode-escape')
return s
c = u'\xc1'
#convert unicode string to hex string
c = re.sub(r'\\',r'0',raw(c))
#base16 int conversion
c = int(c,16)
print "&"+codepoint2name[c]+";"
print "&"+codepoint2name[int(re.sub(r'\\',r'0',raw(u'á')),16)]+";"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment