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

Machavity
  • 30,841
  • 27
  • 92
  • 100
kovac
  • 307
  • 4
  • 11

2 Answers2

6

To convert a string to a byte...

Change:

a = 'haha'

To:

a = b'haha'
K-Log
  • 608
  • 3
  • 18
3

You can also use

a='haha'
print(chardet.detect(a.encode()))
Machavity
  • 30,841
  • 27
  • 92
  • 100