pythonpython-3.xchardet

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.


Solution

  • To convert a string to a byte...

    Change:

    a = 'haha'
    

    To:

    a = b'haha'