我使用 chardet 来测试编码,但出现错误

I use chardet to test encode , but i got error

import chardet 
a='haha'
print(chardet.detect(a))

TypeError: Expected object of type bytes or bytearray, got: < class 'str'>

我只是输入教程中的代码。 我真的想不通到底哪里出了问题。

将字符串转换为字节...

变化:

a = 'haha'

收件人:

a = b'haha'

您也可以使用

a='haha'
print(chardet.detect(a.encode()))