Use XML character references to escape characters. Use XML character references for unprintable or non-ASCII characters, double quotes and ampersands in a string
(text)
| 53 | |
| 54 | |
| 55 | def escape(text): |
| 56 | """Use XML character references to escape characters. |
| 57 | |
| 58 | Use XML character references for unprintable or non-ASCII |
| 59 | characters, double quotes and ampersands in a string |
| 60 | """ |
| 61 | |
| 62 | def fixup(m): |
| 63 | ch = m.group(0) |
| 64 | return "&#" + str(ord(ch)) + ";" |
| 65 | |
| 66 | text = re.sub('[^ -~]|[&"]', fixup, text) |
| 67 | return text if isinstance(text, str) else str(text) |
| 68 | |
| 69 | |
| 70 | def unescape(text): |