Created
October 17, 2013 19:21
-
-
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.
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
xml_2_html_entity_defs = {\ | |
'\xa0' : ' ', | |
'\xa1' : '¡', | |
'\xa2' : '¢', | |
'\xa3' : '£', | |
'\xa4' : '¤', | |
'\xa5' : '¥', | |
'\xa6' : '¦', | |
'\xa7' : '§', | |
'\xa8' : '¨', | |
'\xa9' : '©', | |
'\xaa' : 'ª', | |
'\xab' : '«', | |
'\xac' : '¬', | |
'\xad' : '­', | |
'\xae' : '®', | |
'\xaf' : '¯', | |
'\xb0' : '°', | |
'\xb1' : '±', | |
'\xb2' : '²', | |
'\xb3' : '³', | |
'\xb4' : '´', | |
'\xb5' : 'µ', | |
'\xb6' : '¶', | |
'\xb7' : '·', | |
'\xb8' : '¸', | |
'\xb9' : '¹', | |
'\xba' : 'º', | |
'\xbb' : '»', | |
'\xbc' : '¼', | |
'\xbd' : '½', | |
'\xbe' : '¾', | |
'\xbf' : '¿', | |
'\xc0' : 'À', | |
'\xc1' : 'Á', | |
'\xc2' : 'Â', | |
'\xc3' : 'Ã', | |
'\xc4' : 'Ä', | |
'\xc5' : 'Å', | |
'\xc6' : 'Æ', | |
'\xc7' : 'Ç', | |
'\xc8' : 'È', | |
'\xc9' : 'É', | |
'\xca' : 'Ê', | |
'\xcb' : 'Ë', | |
'\xcc' : 'Ì', | |
'\xcd' : 'Í', | |
'\xce' : 'Î', | |
'\xcf' : 'Ï', | |
'\xd0' : 'Ð', | |
'\xd1' : 'Ñ', | |
'\xd2' : 'Ò', | |
'\xd3' : 'Ó', | |
'\xd4' : 'Ô', | |
'\xd5' : 'Õ', | |
'\xd6' : 'Ö', | |
'\xd7' : '×', | |
'\xd8' : 'Ø', | |
'\xd9' : 'Ù', | |
'\xda' : 'Ú', | |
'\xdb' : 'Û', | |
'\xdc' : 'Ü', | |
'\xdd' : 'Ý', | |
'\xde' : 'Þ', | |
'\xdf' : 'ß', | |
'\xe0' : 'à', | |
'\xe1' : 'á', | |
'\xe2' : 'â', | |
'\xe3' : 'ã', | |
'\xe4' : 'ä', | |
'\xe5' : 'å', | |
'\xe6' : 'æ', | |
'\xe7' : 'ç', | |
'\xe8' : 'è', | |
'\xe9' : 'é', | |
'\xea' : 'ê', | |
'\xeb' : 'ë', | |
'\xec' : 'ì', | |
'\xed' : 'í', | |
'\xee' : 'î', | |
'\xef' : 'ï', | |
'\xf0' : 'ð', | |
'\xf1' : 'ñ', | |
'\xf2' : 'ò', | |
'\xf3' : 'ó', | |
'\xf4' : 'ô', | |
'\xf5' : 'õ', | |
'\xf6' : 'ö', | |
'\xf7' : '÷', | |
'\xf8' : 'ø', | |
'\xf9' : 'ù', | |
'\xfa' : 'ú', | |
'\xfb' : 'û', | |
'\xfc' : 'ü', | |
'\xfd' : 'ý', | |
'\xfe' : 'þ', | |
'\xff' : 'ÿ', | |
'Œ' : 'Œ', | |
'œ' : 'œ', | |
'Š' : 'Š', | |
'š' : 'š', | |
'Ÿ' : 'Ÿ', | |
'ƒ' : 'ƒ', | |
'ˆ' : 'ˆ', | |
'˜' : '˜', | |
' ' : ' ', | |
' ' : ' ', | |
' ' : ' ', | |
'‌' : '‌', | |
'‍' : '‍', | |
'‎' : '‎', | |
'‏' : '‏', | |
'–' : '–', | |
'—' : '—', | |
'‘' : '‘', | |
'’' : '’', | |
'‚' : '‚', | |
'“' : '“', | |
'”' : '”', | |
'„' : '„', | |
'†' : '†', | |
'‡' : '‡', | |
'•' : '•', | |
'…' : '…', | |
'‰' : '‰', | |
'′' : '′', | |
'″' : '″', | |
'‹' : '‹', | |
'›' : '›', | |
'‾' : '‾', | |
'⁄' : '⁄', | |
'€' : '€', | |
'ℑ' : 'ℑ', | |
'℘' : '℘', | |
'ℜ' : 'ℜ', | |
'™' : '™', | |
'ℵ' : 'ℵ', | |
'←' : '←', | |
'↑' : '↑', | |
'→' : '→', | |
'↓' : '↓', | |
'↔' : '↔', | |
'↵' : '↵', | |
'⇐' : '⇐', | |
'⇑' : '⇑', | |
'⇒' : '⇒', | |
'⇓' : '⇓', | |
'⇔' : '⇔', | |
'∀' : '∀', | |
'∂' : '∂', | |
'∃' : '∃', | |
'∅' : '∅', | |
'∇' : '∇', | |
'∈' : '∈', | |
'∉' : '∉', | |
'∋' : '∋', | |
'∏' : '∏', | |
'∑' : '∑', | |
'−' : '−', | |
'∗' : '∗', | |
'√' : '√', | |
'∝' : '∝', | |
'∞' : '∞', | |
'∠' : '∠', | |
'∧' : '∧', | |
'∨' : '∨', | |
'∩' : '∩', | |
'∪' : '∪', | |
'∫' : '∫', | |
'∴' : '∴', | |
'∼' : '∼', | |
'≅' : '≅', | |
'≈' : '≈', | |
'≠' : '≠', | |
'≡' : '≡', | |
'≤' : '≤', | |
'≥' : '≥', | |
'⊂' : '⊂', | |
'⊃' : '⊃', | |
'⊄' : '⊄', | |
'⊆' : '⊆', | |
'⊇' : '⊇', | |
'⊕' : '⊕', | |
'⊗' : '⊗', | |
'⊥' : '⊥', | |
'⋅' : '⋅', | |
'⌈' : '⌈', | |
'⌉' : '⌉', | |
'⌊' : '⌊', | |
'⌋' : '⌋', | |
'〈' : '⟨', | |
'〉' : '⟩', | |
'Α' : 'Α', | |
'Β' : 'Β', | |
'Γ' : 'Γ', | |
'Δ' : 'Δ', | |
'Ε' : 'Ε', | |
'Ζ' : 'Ζ', | |
'Η' : 'Η', | |
'Θ' : 'Θ', | |
'Ι' : 'Ι', | |
'Κ' : 'Κ', | |
'Λ' : 'Λ', | |
'Μ' : 'Μ', | |
'Ν' : 'Ν', | |
'Ξ' : 'Ξ', | |
'Ο' : 'Ο', | |
'Π' : 'Π', | |
'Ρ' : 'Ρ', | |
'Σ' : 'Σ', | |
'Τ' : 'Τ', | |
'Υ' : 'Υ', | |
'Φ' : 'Φ', | |
'Χ' : 'Χ', | |
'Ψ' : 'Ψ', | |
'Ω' : 'Ω', | |
'α' : 'α', | |
'β' : 'β', | |
'γ' : 'γ', | |
'δ' : 'δ', | |
'ε' : 'ε', | |
'ζ' : 'ζ', | |
'η' : 'η', | |
'θ' : 'θ', | |
'ι' : 'ι', | |
'κ' : 'κ', | |
'λ' : 'λ', | |
'μ' : 'μ', | |
'ν' : 'ν', | |
'ξ' : 'ξ', | |
'ο' : 'ο', | |
'π' : 'π', | |
'ρ' : 'ρ', | |
'ς' : 'ς', | |
'σ' : 'σ', | |
'τ' : 'τ', | |
'υ' : 'υ', | |
'φ' : 'φ', | |
'χ' : 'χ', | |
'◊' : '◊', | |
'ψ' : 'ψ', | |
'ω' : 'ω', | |
'ϑ' : 'ϑ', | |
'ϒ' : 'ϒ', | |
'ϖ' : 'ϖ', | |
'♠' : '♠', | |
'♣' : '♣', | |
'♥' : '♥', | |
'♦' : '♦', | |
} | |
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