Skip to content

Instantly share code, notes, and snippets.

@arnaudsj
Created October 28, 2009 15:24
Show Gist options
  • Save arnaudsj/220537 to your computer and use it in GitHub Desktop.
Save arnaudsj/220537 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2.6
# -*- coding: utf-8 -*-
"""
Code snippet that shows how to convert a utf-8 encoded string to an XML decimal entity string
"""
from xml.sax import saxutils
import codecs
if __name__ == "__main__":
# Example string to convert
s = "Пожалуйста, ответьте на все вопросы, выделенные красным, прежде чем отправлять свой отзыв. Спасибо!"
# For the output to be us-ascii
enc = codecs.getencoder('us-ascii')
# Perform the re-encoding
print enc(saxutils.escape(s).decode('utf-8','ignore'), 'xmlcharrefreplace')[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment