code: def utf8_encode_str(string, encoding='UTF-8'):
      
      if not string:
          return ''
      src_enc = chardet.detect(string)['encoding']
      try:
          return string.decode(src_enc).encode(encoding)
      except:
          return string.decode('ascii', errors='replace').encode(encoding)
docs: Attempt to detect the native encoding of `string`, and re-encode
      to utf-8
  
      :type string: str
      :param string: The string to be encoded.
  
      :rtype: str
      :returns: A utf-8 encoded string.