Python 将字符映射到其转义版本

Python 将字符映射到其转义版本,python,Python,我有以下映射: mapping = {'a': '\a', 'b': '\b', 'f': '\f', 'n': '\n', 'r': '\r', 't': '\t', 'v': '\v'} 有没有办法不用字典就能做到这一点?也许是这样的: if c in "abfnrtv": c = '\\' + c 是2.x。在3.x上使用字节s和'unicode-escape'3>>(b'\'+b'a')。解码('unicode-escape')'\x07'您将如何以便携方式

我有以下映射:

mapping = {'a': '\a', 'b': '\b', 'f': '\f', 'n': '\n', 'r': '\r', 
            't': '\t', 'v': '\v'}
有没有办法不用字典就能做到这一点?也许是这样的:

if c in "abfnrtv": c = '\\' + c

是2.x。在3.x上使用
字节
s和
'unicode-escape'
<代码>3>>(b'\'+b'a')。解码('unicode-escape')
'\x07'
您将如何以便携方式执行此操作?取决于您需要的便携程度。2.6+具有
字节
>>> ('\\' + 'a').decode('string-escape')
'\x07'