Created
September 27, 2012 22:46
-
-
Save marcoslhc/3796906 to your computer and use it in GitHub Desktop.
generate html entity codes
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
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