无法转义字符串中的转义字符
发布于 2021-01-29 15:04:40
为了回答这个问题,我设法print
通过转义反斜杠来使字符串成为转义字符。
当我尝试将其概括为转义所有转义的字符时,它似乎无能为力:
>>> a = "word\nanother word\n\tthird word"
>>> a
'word\nanother word\n\tthird word'
>>> print a
word
another word
third word
>>> b = a.replace("\\", "\\\\")
>>> b
'word\nanother word\n\tthird word'
>>> print b
word
another word
third word
但是对于特定的转义字符使用相同的方法,它确实起作用:
>>> b = a.replace('\n', '\\n')
>>> print b
word\nanother word\n third word
>>> b
'word\\nanother word\\n\tthird word'
有一般的方法可以做到这一点吗?应包括\n
,\t
,\r
,等。
关注者
0
被浏览
89
1 个回答