如何在Python中处理字符串中的转义序列?
Python中有两种方法可以处理反斜杠转义的字符串。首先是使用literal_eval来评估字符串。请注意,在此方法中,您需要在另一层引号中括起字符串。例如:
>>> import ast
>>> a = '"Hello,\nworld"'
>>> print ast.literal_eval(a)
Hello,
world
另一种方法是使用string类中的decode(‘string_escape’)方法。例如,
>>> print "Hello,\nworld".decode('string_escape')
Hello,
world
阅读更多:Python 教程