Reverse repr function in Python

Question:

if I have a string with characters ( 0x61 0x62 0xD ), the repr function of this string will return 'abr'.

Is there way to do reverse operation: if I have string 'abr' (with characters 0x61 0x62 0x5C 0x72), I need obtain string 0x61 0x62 0xD.

Asked By: Ivan Borshchov

||

Answers:

I think what you’re looking for is ast.literal_eval:

>>> s = repr("abr")
>>> s
"'ab\r'"
>>> from ast import literal_eval
>>> literal_eval(s)
'abr'
Answered By: jonrsharpe
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.