def smart_decode(s):
 if isinstance(s, unicode):
  return s
 enc_guess = chardet.detect(s)
 try:
  return s.decode(enc_guess['encoding'])
 except (UnicodeDecodeError, TypeError) as exc:
  msg = 'Error decoding string (encoding: %r %.2f%% sure): %s: %s'
  log.error((msg % (enc_guess['encoding'], (enc_guess['confidence'] * 100.0), exc.__class__.__name__, exc)))
  return unicode(s, errors='replace')

def smart_decode ( s ) : if isinstance ( s , unicode ) : return s enc_guess = chardet . detect ( s ) try : return s . decode ( enc_guess [ 'encoding' ] ) except ( UnicodeDecodeError , TypeError ) as exc : msg = 'Error decoding string (encoding: %r %.2f%% sure): %s: %s' log . error ( ( msg % ( enc_guess [ 'encoding' ] , ( enc_guess [ 'confidence' ] * 100.0 ) , exc . __class__ . __name__ , exc ) ) ) return unicode ( s , errors = 'replace' )
guess the encoding of a string and decode it .