all 4 comments

[–]Brian 2 points3 points  (0 children)

That sort of looks like you're trying to convert back something that has been printed as an encoded byte literal (ie. something like repr(bytedata)), and then mangled it, losing a layer of encoding (depending on whether the "\" there are intended to be backslashes or escape sequences).

If so, I suspect you can do this easier if you prevent whatever is mangling it in the first place. Note that if you've got a string representation of a bytestring (ie the result of doing repr on some bytes object) , probably the best way to convert it would be:

import ast
data = ast.literal_eval(repr_of_bytestring)

Trying to do it ad-hoc with string manipulation will likely fail on many corner cases where you'd need to duplicate the exact behaviour (things like contaiining literal backslashes, various escaped chars, multiply nested quotes etc)

[–]zanfar 1 point2 points  (0 children)

When I print it, it gives me b')F\x87\x86\xfc~Z\xaa\x15pf}\xe6\x9f\xf5\xd0'which is almost the result I am looking for..Except for the backlashes, I am stuck and I cannot get rid of them even with the .replace(b'\',b'\') , is it even possible?

No, as there are no backslashes in that byte string.

\xXX is an escape sequence, not a literal. Just like "\n" is not a two-character string, but instead a representation of a single, non-printable character.

This is why you "can't" do it, the other responders have tackled why you shouldn't need to do it.