I use chardet to test encode , but i got error

10.3k views Asked by At
import chardet 
a='haha'
print(chardet.detect(a))

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

I just type code from tutorial. I really can not figure out what wrong happended.

2

There are 2 answers

0
K-Log On BEST ANSWER

To convert a string to a byte...

Change:

a = 'haha'

To:

a = b'haha'
0
Hanbing Sun On

You can also use

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