kovac
kovac

Reputation: 317

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'>

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

Upvotes: 6

Views: 11055

Answers (2)

Hanbing Sun
Hanbing Sun

Reputation: 31

You can also use

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

Upvotes: 3

K-Log
K-Log

Reputation: 618

To convert a string to a byte...

Change:

a = 'haha'

To:

a = b'haha'

Upvotes: 6

Related Questions