Skip to content

Instantly share code, notes, and snippets.

@miebach
Created December 5, 2011 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miebach/1433874 to your computer and use it in GitHub Desktop.
Save miebach/1433874 to your computer and use it in GitHub Desktop.
Guess the encoding of a python string
def decode_guess(s, etypes=('utf8','latin1','ascii')):
"""
Returns an unicode object
"""
for et in etypes:
try:
return s.decode(et)
except UnicodeDecodeError:
pass
return s.decode('ascii', 'ignore')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment