[英]How to prevent automatic escaping of special characters in Python
I'm writing a Python script that accepts file paths as strings, parses them, appends a command name, and builds a list, which is then passed to subprocess.Popen()
for execution. 我正在编写一个Python脚本,它接受文件路径作为字符串,解析它们,附加命令名称,并构建一个列表,然后传递给subprocess.Popen()
以供执行。 This script is to handle both Unix and Windows file paths, and ultimately should run on both systems. 此脚本用于处理Unix和Windows文件路径,最终应在两个系统上运行。
When I run this under Unix, if I give a Windows path that inadvertently contains an escape character (eg \\Users\\Administrator\\bin
), Python will interpret the embedded \\b
as the backspace character. 当我在Unix下运行时,如果我给出一个无意中包含转义字符的Windows路径(例如\\Users\\Administrator\\bin
),Python会将嵌入的\\b
解释为退格字符。 I want to prevent that from happening. 我想防止这种情况发生。
As far as I know, there's no function or method to denote a string variable as a raw string. 据我所知,没有函数或方法将字符串变量表示为原始字符串。 The 'r'
modifier only works for string constants. 'r'
修饰符仅适用于字符串常量。
So far, the closest I've been able to get is this: 到目前为止,我能得到的最接近的是:
winpath = "C:\Users\Administrator\bin"
winpath = winpath.replace('\b','\\b')
winpathlist = winpath.split('\\')
At this point, winpathlist should contain ['C:','Users','Administrator','bin']
, not ['C','Users','Administrator\\x08in']
. 此时,winpathlist应包含['C:','Users','Administrator','bin']
,而不是['C','Users','Administrator\\x08in']
。
I can add additional calls to winpath.replace()
to handle the other escapes I might get -- \\a
, \\f
, \\n
, \\r
, \\t
, \\v
-- but not \\x
. 我可以添加额外的调用winpath.replace()
来处理我可能获得的其他转义 - \\a
, \\f
, \\n
, \\r
, \\t
, \\v
\\t
- 但不是\\x
。
Is there a more pythonic way to do this? 是否有更多的pythonic方式来做到这一点?
If your winpath
is hard-coded, you may want to use r
before your string to indicate it is a "raw string" . 如果您的winpath
是硬编码的,您可能希望在字符串之前使用r
来表示它是一个“原始字符串” 。
winpath = r"C:\Users\Administrator\bin"
If winpath
cannot be hardcoded, you can try to create a new string as: 如果winpath
无法硬编码,您可以尝试创建一个新字符串:
escaped_winpath = "%r" % winpath
(which is just repr(winpath)
, and won't really help you, as repr("\\bin")
is...) (这只是repr(winpath)
,并不会真正帮助你,因为repr("\\bin")
是...)
A solution would be to rebuild the string from scratch: you can find an example of function at that link , but the generic idea is: 解决方案是从头开始重建字符串:您可以在该链接上找到函数的示例,但通用的想法是:
escape_dict={'\a':r'\a',
'\b':r'\b',
'\c':r'\c',
'\f':r'\f',
'\n':r'\n',
'\r':r'\r',
'\t':r'\t',
'\v':r'\v',
'\'':r'\'',
'\"':r'\"'}
def raw(text):
"""Returns a raw string representation of text"""
new_string=''
for char in text:
try:
new_string += escape_dict[char]
except KeyError:
new_string += char
return new_string
and now, raw("\\bin")
gives you "\\\\bin"
(and not "\\\\x08in"
)... 现在, raw("\\bin")
给你"\\\\bin"
(而不是"\\\\x08in"
)......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.