Skip to content

Instantly share code, notes, and snippets.

@technige
Created June 16, 2014 11:29
Show Gist options
  • Save technige/5901ecf23baa6f63c79a to your computer and use it in GitHub Desktop.
Save technige/5901ecf23baa6f63c79a to your computer and use it in GitHub Desktop.
try:
unicode
except NameError:
# Python 3
def ustr(s, encoding="utf-8"):
if isinstance(s, str):
return s
try:
return s.decode(encoding)
except AttributeError:
return str(s)
else:
# Python 2
def ustr(s, encoding="utf-8"):
if isinstance(s, str):
return s.decode(encoding)
else:
return unicode(s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment