code: def safe_print(msg):
      
      try:
          print(msg)
      except UnicodeEncodeError:
          try:
              # NOTE encoding and decoding so that in Python 3 no b"..." is printed
              encoded = msg.encode(sys.stdout.encoding, "replace")
              decoded = encoded.decode(sys.stdout.encoding, "replace")
              print(decoded)
          except (UnicodeDecodeError, UnicodeEncodeError):
              print(u"[ERRO] An unexpected error happened while printing to stdout.")
              print(u"[ERRO] Please check that your file/string encoding matches the shell encoding.")
              print(u"[ERRO] If possible, set your shell encoding to UTF-8 and convert any files with legacy encodings.")
docs: Safely print a given Unicode string to stdout,
      possibly replacing characters non-printable
      in the current stdout encoding.
  
      :param string msg: the message