code: def sanitize(meta, diagnostics=False):
      
      bad_encodings, bad_fields = set(), set()
  
      def sane_encoding(field, text):
          "Transcoding helper."
          for encoding in ('utf-8', meta.get('encoding', None), 'cp1252'):
              if encoding:
                  try:
                      u8_text = text.decode(encoding).encode("utf-8")
                      if encoding != 'utf-8':
                          bad_encodings.add(encoding)
                          bad_fields.add(field)
                      return u8_text
                  except UnicodeError:
                      continue
          else:
              # Broken beyond anything reasonable
              bad_encodings.add('UNKNOWN/EXOTIC')
              bad_fields.add(field)
              return str(text, 'utf-8', 'replace').replace('\ufffd', '_').encode("utf-8")
  
      # Go through all string fields and check them
      for field in ("comment", "created by"):
          if field in meta:
              meta[field] = sane_encoding(field, meta[field])
  
      meta["info"]["name"] = sane_encoding('info name', meta["info"]["name"])
  
      for entry in meta["info"].get("files", []):
          entry["path"] = [sane_encoding('file path', i) for i in entry["path"]]
  
      return (meta, bad_encodings, bad_fields) if diagnostics else meta
docs: Try to fix common problems, especially transcode non-standard string encodings.