def guess_encoding(data):
 successful_encoding = None
 encodings = ['utf-8']
 try:
  encodings.append(locale.nl_langinfo(locale.CODESET))
 except AttributeError:
  pass
 try:
  encodings.append(locale.getlocale()[1])
 except (AttributeError, IndexError):
  pass
 try:
  encodings.append(locale.getdefaultlocale()[1])
 except (AttributeError, IndexError):
  pass
 encodings.append('latin-1')
 for enc in encodings:
  if (not enc):
   continue
  try:
   decoded = text_type(data, enc)
   successful_encoding = enc
  except (UnicodeError, LookupError):
   pass
  else:
   break
 if (not successful_encoding):
  raise UnicodeError(('Unable to decode input data.  Tried the following encodings: %s.' % ', '.join([repr(enc) for enc in encodings if enc])))
 else:
  return (decoded, successful_encoding)

def guess_encoding ( data ) : successful_encoding = None encodings = [ 'utf-8' ] try : encodings . append ( locale . nl_langinfo ( locale . CODESET ) ) except AttributeError : pass try : encodings . append ( locale . getlocale ( ) [ 1 ] ) except ( AttributeError , IndexError ) : pass try : encodings . append ( locale . getdefaultlocale ( ) [ 1 ] ) except ( AttributeError , IndexError ) : pass encodings . append ( 'latin-1' ) for enc in encodings : if ( not enc ) : continue try : decoded = text_type ( data , enc ) successful_encoding = enc except ( UnicodeError , LookupError ) : pass else : break if ( not successful_encoding ) : raise UnicodeError ( ( 'Unable to decode input data.  Tried the following encodings: %s.' % ', ' . join ( [ repr ( enc ) for enc in encodings if enc ] ) ) ) else : return ( decoded , successful_encoding )
given a byte string .