Skip to content

Instantly share code, notes, and snippets.

@jvanasco
Created October 17, 2013 19:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jvanasco/7030697 to your computer and use it in GitHub Desktop.
Save jvanasco/7030697 to your computer and use it in GitHub Desktop.
i needed to turn unicode text into ascii with html entity defs. in order to generate the `xml_2_html_entity_defs` , i used `pprint` to grab the entities from python's `htmlentitydefs` package , sorted the list, removed some troublesome entities ( `<`, `>`, `"`, etc ), and the formatted values to correctly wrap in the `&;` syntax.
xml_2_html_entity_defs = {\
'\xa0' : '&nbsp;',
'\xa1' : '&iexcl;',
'\xa2' : '&cent;',
'\xa3' : '&pound;',
'\xa4' : '&curren;',
'\xa5' : '&yen;',
'\xa6' : '&brvbar;',
'\xa7' : '&sect;',
'\xa8' : '&uml;',
'\xa9' : '&copy;',
'\xaa' : '&ordf;',
'\xab' : '&laquo;',
'\xac' : '&not;',
'\xad' : '&shy;',
'\xae' : '&reg;',
'\xaf' : '&macr;',
'\xb0' : '&deg;',
'\xb1' : '&plusmn;',
'\xb2' : '&sup2;',
'\xb3' : '&sup3;',
'\xb4' : '&acute;',
'\xb5' : '&micro;',
'\xb6' : '&para;',
'\xb7' : '&middot;',
'\xb8' : '&cedil;',
'\xb9' : '&sup1;',
'\xba' : '&ordm;',
'\xbb' : '&raquo;',
'\xbc' : '&frac14;',
'\xbd' : '&frac12;',
'\xbe' : '&frac34;',
'\xbf' : '&iquest;',
'\xc0' : '&Agrave;',
'\xc1' : '&Aacute;',
'\xc2' : '&Acirc;',
'\xc3' : '&Atilde;',
'\xc4' : '&Auml;',
'\xc5' : '&Aring;',
'\xc6' : '&AElig;',
'\xc7' : '&Ccedil;',
'\xc8' : '&Egrave;',
'\xc9' : '&Eacute;',
'\xca' : '&Ecirc;',
'\xcb' : '&Euml;',
'\xcc' : '&Igrave;',
'\xcd' : '&Iacute;',
'\xce' : '&Icirc;',
'\xcf' : '&Iuml;',
'\xd0' : '&ETH;',
'\xd1' : '&Ntilde;',
'\xd2' : '&Ograve;',
'\xd3' : '&Oacute;',
'\xd4' : '&Ocirc;',
'\xd5' : '&Otilde;',
'\xd6' : '&Ouml;',
'\xd7' : '&times;',
'\xd8' : '&Oslash;',
'\xd9' : '&Ugrave;',
'\xda' : '&Uacute;',
'\xdb' : '&Ucirc;',
'\xdc' : '&Uuml;',
'\xdd' : '&Yacute;',
'\xde' : '&THORN;',
'\xdf' : '&szlig;',
'\xe0' : '&agrave;',
'\xe1' : '&aacute;',
'\xe2' : '&acirc;',
'\xe3' : '&atilde;',
'\xe4' : '&auml;',
'\xe5' : '&aring;',
'\xe6' : '&aelig;',
'\xe7' : '&ccedil;',
'\xe8' : '&egrave;',
'\xe9' : '&eacute;',
'\xea' : '&ecirc;',
'\xeb' : '&euml;',
'\xec' : '&igrave;',
'\xed' : '&iacute;',
'\xee' : '&icirc;',
'\xef' : '&iuml;',
'\xf0' : '&eth;',
'\xf1' : '&ntilde;',
'\xf2' : '&ograve;',
'\xf3' : '&oacute;',
'\xf4' : '&ocirc;',
'\xf5' : '&otilde;',
'\xf6' : '&ouml;',
'\xf7' : '&divide;',
'\xf8' : '&oslash;',
'\xf9' : '&ugrave;',
'\xfa' : '&uacute;',
'\xfb' : '&ucirc;',
'\xfc' : '&uuml;',
'\xfd' : '&yacute;',
'\xfe' : '&thorn;',
'\xff' : '&yuml;',
'&#338;' : '&OElig;',
'&#339;' : '&oelig;',
'&#352;' : '&Scaron;',
'&#353;' : '&scaron;',
'&#376;' : '&Yuml;',
'&#402;' : '&fnof;',
'&#710;' : '&circ;',
'&#732;' : '&tilde;',
'&#8194;' : '&ensp;',
'&#8195;' : '&emsp;',
'&#8201;' : '&thinsp;',
'&#8204;' : '&zwnj;',
'&#8205;' : '&zwj;',
'&#8206;' : '&lrm;',
'&#8207;' : '&rlm;',
'&#8211;' : '&ndash;',
'&#8212;' : '&mdash;',
'&#8216;' : '&lsquo;',
'&#8217;' : '&rsquo;',
'&#8218;' : '&sbquo;',
'&#8220;' : '&ldquo;',
'&#8221;' : '&rdquo;',
'&#8222;' : '&bdquo;',
'&#8224;' : '&dagger;',
'&#8225;' : '&Dagger;',
'&#8226;' : '&bull;',
'&#8230;' : '&hellip;',
'&#8240;' : '&permil;',
'&#8242;' : '&prime;',
'&#8243;' : '&Prime;',
'&#8249;' : '&lsaquo;',
'&#8250;' : '&rsaquo;',
'&#8254;' : '&oline;',
'&#8260;' : '&frasl;',
'&#8364;' : '&euro;',
'&#8465;' : '&image;',
'&#8472;' : '&weierp;',
'&#8476;' : '&real;',
'&#8482;' : '&trade;',
'&#8501;' : '&alefsym;',
'&#8592;' : '&larr;',
'&#8593;' : '&uarr;',
'&#8594;' : '&rarr;',
'&#8595;' : '&darr;',
'&#8596;' : '&harr;',
'&#8629;' : '&crarr;',
'&#8656;' : '&lArr;',
'&#8657;' : '&uArr;',
'&#8658;' : '&rArr;',
'&#8659;' : '&dArr;',
'&#8660;' : '&hArr;',
'&#8704;' : '&forall;',
'&#8706;' : '&part;',
'&#8707;' : '&exist;',
'&#8709;' : '&empty;',
'&#8711;' : '&nabla;',
'&#8712;' : '&isin;',
'&#8713;' : '&notin;',
'&#8715;' : '&ni;',
'&#8719;' : '&prod;',
'&#8721;' : '&sum;',
'&#8722;' : '&minus;',
'&#8727;' : '&lowast;',
'&#8730;' : '&radic;',
'&#8733;' : '&prop;',
'&#8734;' : '&infin;',
'&#8736;' : '&ang;',
'&#8743;' : '&and;',
'&#8744;' : '&or;',
'&#8745;' : '&cap;',
'&#8746;' : '&cup;',
'&#8747;' : '&int;',
'&#8756;' : '&there4;',
'&#8764;' : '&sim;',
'&#8773;' : '&cong;',
'&#8776;' : '&asymp;',
'&#8800;' : '&ne;',
'&#8801;' : '&equiv;',
'&#8804;' : '&le;',
'&#8805;' : '&ge;',
'&#8834;' : '&sub;',
'&#8835;' : '&sup;',
'&#8836;' : '&nsub;',
'&#8838;' : '&sube;',
'&#8839;' : '&supe;',
'&#8853;' : '&oplus;',
'&#8855;' : '&otimes;',
'&#8869;' : '&perp;',
'&#8901;' : '&sdot;',
'&#8968;' : '&lceil;',
'&#8969;' : '&rceil;',
'&#8970;' : '&lfloor;',
'&#8971;' : '&rfloor;',
'&#9001;' : '&lang;',
'&#9002;' : '&rang;',
'&#913;' : '&Alpha;',
'&#914;' : '&Beta;',
'&#915;' : '&Gamma;',
'&#916;' : '&Delta;',
'&#917;' : '&Epsilon;',
'&#918;' : '&Zeta;',
'&#919;' : '&Eta;',
'&#920;' : '&Theta;',
'&#921;' : '&Iota;',
'&#922;' : '&Kappa;',
'&#923;' : '&Lambda;',
'&#924;' : '&Mu;',
'&#925;' : '&Nu;',
'&#926;' : '&Xi;',
'&#927;' : '&Omicron;',
'&#928;' : '&Pi;',
'&#929;' : '&Rho;',
'&#931;' : '&Sigma;',
'&#932;' : '&Tau;',
'&#933;' : '&Upsilon;',
'&#934;' : '&Phi;',
'&#935;' : '&Chi;',
'&#936;' : '&Psi;',
'&#937;' : '&Omega;',
'&#945;' : '&alpha;',
'&#946;' : '&beta;',
'&#947;' : '&gamma;',
'&#948;' : '&delta;',
'&#949;' : '&epsilon;',
'&#950;' : '&zeta;',
'&#951;' : '&eta;',
'&#952;' : '&theta;',
'&#953;' : '&iota;',
'&#954;' : '&kappa;',
'&#955;' : '&lambda;',
'&#956;' : '&mu;',
'&#957;' : '&nu;',
'&#958;' : '&xi;',
'&#959;' : '&omicron;',
'&#960;' : '&pi;',
'&#961;' : '&rho;',
'&#962;' : '&sigmaf;',
'&#963;' : '&sigma;',
'&#964;' : '&tau;',
'&#965;' : '&upsilon;',
'&#966;' : '&phi;',
'&#967;' : '&chi;',
'&#9674;' : '&loz;',
'&#968;' : '&psi;',
'&#969;' : '&omega;',
'&#977;' : '&thetasym;',
'&#978;' : '&upsih;',
'&#982;' : '&piv;',
'&#9824;' : '&spades;',
'&#9827;' : '&clubs;',
'&#9829;' : '&hearts;',
'&#9830;' : '&diams;',
}
def unicode_to_ascii_html( text )
clean = text.encode('ascii', 'xmlcharrefreplace')
for (k,v) in xml_2_html_entity_defs.items() :
clean = clean.replace( k , v )
return clean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment