Reverse repr function in Python

南笙酒味 提交于 2019-11-28 03:11:20

问题


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

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


回答1:


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

>>> s = repr("ab\r")
>>> s
"'ab\\r'"
>>> from ast import literal_eval
>>> literal_eval(s)
'ab\r'


来源:https://stackoverflow.com/questions/24886123/reverse-repr-function-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!