将常规 Python 字符串转换为原始字符串 [英] Convert regular Python string to raw string

查看:91
本文介绍了将常规 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

这篇关于将常规 Python 字符串转换为原始字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆