12345678def test(): import chardet data = b"\xe4\xb8\xad\xe6\x96\x87" if isinstance(data, bytes): encoding = chardet.detect(data)["encoding"] print("encoding", encoding) text = data.decode(encoding, "ignore") print("text", text)