Skip to content

Instantly share code, notes, and snippets.

@gnrfan
Created May 31, 2011 00:17
Show Gist options
  • Save gnrfan/999666 to your computer and use it in GitHub Desktop.
Save gnrfan/999666 to your computer and use it in GitHub Desktop.
Python snippet: Escape unicode characters to be included in XML elements
#!/usr/bin/env python
# -*- encoding: utf8 -*-
def escape_unicode_for_xml(str):
replacements = (
('<', '&lt;'),
('>', '&gt;'),
('"', '&quot;'),
)
for orig, rep in replacements:
str = str.replace(orig, rep)
return str
if __name__ == '__main__':
print escape_unicode_for_xml("Juan Pérez")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment