How to detect string byte encoding?

Use chardet library. It is super easy

import chardet

the_encoding = chardet.detect('your string')['encoding']

and that’s it!

in python3 you need to provide type bytes or bytearray so:

import chardet
the_encoding = chardet.detect(b'your string')['encoding']

Leave a Comment