2014-07-09 9 views
1

我有這樣的映射:地圖字符轉義其版本

mapping = {'a': '\a', 'b': '\b', 'f': '\f', 'n': '\n', 'r': '\r', 
      't': '\t', 'v': '\v'} 

有沒有辦法做到這一點不使用詞典?也許是這樣的:

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

回答

3
>>> ('\\' + 'a').decode('string-escape') 
'\x07' 
+0

thanks!這個版本是否依賴? – perreal

+1

它是2.x.在3.x上使用'byte's和''unicode-escape''代替。 '3 >>(b'\\'+ b'a')。decode('unicode-escape')' ''\ x07'' –

+0

你會如何以便攜的方式去做這件事? – perreal