将常规 Python 字符串转换为原始字符串
问题描述:
我有一个字符串s
,它的内容是可变的.我想把它变成一个原始字符串.我该怎么做?
I have a string s
, its contents are variable. I'd like to make it a raw string. How do I go about this?
类似于 r''
方法.
答
我相信您正在寻找的是 str.encode("string-escape") 函数.例如,如果您有一个想要原始字符串"的变量:
i believe what you're looking for is the str.encode("string-escape") function. For example, if you have a variable that you want to 'raw string':
a = '\x89'
a.encode('unicode_escape')
'\\x89'
注意:使用 string-escape
用于 python 2.x 和旧版本
Note: Use string-escape
for python 2.x and older versions
我正在寻找类似的解决方案,并通过以下方式找到了解决方案:转换原始字符串 python
I was searching for a similar solution and found the solution via: casting raw strings python