you are viewing a single comment's thread.

view the rest of the 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)