Python中的反向repr函数 [英] Reverse repr function in Python
本文介绍了Python中的反向repr函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有一个包含字符(0x61
0x62
0xD
)的字符串,则此字符串的repr
函数将返回'ab\r'
.
if I have a string with characters ( 0x61
0x62
0xD
), the repr
function of this string will return 'ab\r'
.
是否可以执行反向操作:如果我有字符串'ab\r'
(字符为0x61
0x62
0x5C
0x72
),则需要获取字符串0x61
0x62
0xD
./p>
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
.
推荐答案
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'
这篇关于Python中的反向repr函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文