Created
September 6, 2012 10:22
-
-
Save saevarom/3654490 to your computer and use it in GitHub Desktop.
To unicode or bust
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
# 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