code: def guess_minimum_encoding(text, charsets=('ascii', 'latin1', 'utf8')):
      
      text_in_unicode = text.decode('utf8', 'replace')
      for charset in charsets:
          try:
              return (text_in_unicode.encode(charset), charset)
          except (UnicodeEncodeError, UnicodeDecodeError):
              pass
      return (text_in_unicode.encode('utf8'), 'utf8')
docs: Try to guess the minimum charset that is able to represent.
  
      Try to guess the minimum charset that is able to represent the given
      text using the provided charsets. text is supposed to be encoded in utf8.
      Returns (encoded_text, charset) where charset is the first charset
      in the sequence being able to encode text.
      Returns (text_in_utf8, 'utf8') in case no charset is able to encode text.
  
      @note: If the input text is not in strict UTF-8, then replace any
          non-UTF-8 chars inside it.