Skip to content

Instantly share code, notes, and snippets.

@saevarom
Created September 6, 2012 10:22
Show Gist options
  • Save saevarom/3654490 to your computer and use it in GitHub Desktop.
Save saevarom/3654490 to your computer and use it in GitHub Desktop.
To unicode or bust
# From "Unicode in Python, Completely Demystified" by Kumar McMillan.
# http://farmdev.com/talks/unicode/
def to_unicode_or_bust(
obj, encoding='utf-8'):
if isinstance(obj, basestring):
if not isinstance(obj, unicode):
obj = unicode(obj, encoding)
return obj
>>> to_unicode_or_bust(ivan_uni)
u'Ivan Krsti\u0107'
>>> to_unicode_or_bust(ivan_utf8)
u'Ivan Krsti\u0107'
>>> to_unicode_or_bust(1234)
1234
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment