Is there any way we can find what kind of encoding is used in bytes string with codecs in python. There is a method in chardet chardet.detect(string)['encoding']
Is there any method similar to this in codecs python
Asked
Active
Viewed 207 times
0

Sarthak Kaushik
- 19
- 1
- 3
-
Why not use chardet? – Thaer A May 02 '20 at 08:08
-
If there was such a method in the standard library, chardet would most probably not exist. – MaxNoe May 02 '20 at 08:29
-
Does this answer your question? [How to detect string byte encoding?](https://stackoverflow.com/questions/15918314/how-to-detect-string-byte-encoding) – Joe May 02 '20 at 09:15
1 Answers
0
There isn't a built-in method, because it wouldn't be possible to reliably determine this for arbitrary values and arbitrary encodings. (For example, any text containing only ASCII characters is valid in most other encodings.)
The best you could do is a series of try-catch blocks where you guess a series of encodings (eg UTF8, UTF16) and go to the next if there is an invalid character.

Christoph Burschka
- 4,467
- 3
- 16
- 31