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

kov*_*vac 2 python python-3.x chardet

import chardet 
a='haha'
print(chardet.detect(a))
Run Code Online (Sandbox Code Playgroud)

类型错误:字节或字节数组类型的预期对象,得到:< class 'str'>

我只是输入教程中的代码。我真的无法弄清楚发生了什么错误。

K-L*_*Log 5

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

改变:

a = 'haha'
Run Code Online (Sandbox Code Playgroud)

到:

a = b'haha'
Run Code Online (Sandbox Code Playgroud)