【发布时间】:2011-10-15 14:31:47
【问题描述】:
我想将包含转义字符的字符串转换为正常形式,就像 Python 的词法解析器一样:
>>> escaped_str = 'One \\\'example\\\''
>>> print(escaped_str)
One \'Example\'
>>> normal_str = normalize_str(escaped_str)
>>> print(normal_str)
One 'Example'
当然,无聊的方法是一一替换所有已知的转义字符: http://docs.python.org/reference/lexical_analysis.html#string-literals
你会如何在上面的代码中实现normalize_str()?
【问题讨论】: