I use chardet to test encode , but i got error
Asked Answered
D

2

6
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.

Disadvantaged answered 17/5, 2018 at 2:54 Comment(1)
Nothing has been encoded.Kalpak
B
6

To convert a string to a byte...

Change:

a = 'haha'

To:

a = b'haha'
Bedaub answered 17/5, 2018 at 4:49 Comment(0)
R
3

You can also use

a='haha'
print(chardet.detect(a.encode()))
Referential answered 15/7, 2020 at 2:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.