You can not.
It is impossible to determine if "A \ u0026B" was originally written text, or the data is simply bytes of "A \ u0026B", or if we arrived there from some other encoding.
How ... do you know whether to run .decode("unicode-escape")
You must know if someone has previously text.encode('unicode-escape') . Bytes themselves cannot tell you.
Of course, you can guess by looking for escape sequences \ u or \ U, or just try / other than decoding and see what happens, but I do not recommend going along this route.
If you encounter bytestring in your application and you still don't know what encoding is, then your problem lies elsewhere and needs to be fixed elsewhere.
source share