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
.
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'
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
.
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'