如何防止在Python中自动转义特殊字符 [英] How to prevent automatic escaping of special characters in Python
问题描述
subprocess.Popen()
执行。这个脚本是处理Unix和Windows文件路径,最终应该运行在这两个系统上。 当我在Unix下运行这个文件时,如果我给出一个无意中包含转义字符的Windows路径(例如 \Users\Administrator\ bin
),Python将解释嵌入的 \b
作为退格字符。我想防止这种情况发生。
据我所知,没有函数或方法将字符串变量表示为原始字符串。
到目前为止,我最近能够get是这样的:
winpath =C:\Users\Administrator\bin
$ p $此时,winpathlist应包含
winpath = winpath .replace('\b','\\b')
winpathlist = winpath.split('\\')
['C:','Users','Administrator','bin']
,而不是['C','Users','Administrator\x08in']
。
我可以添加额外的调用$ code> winpath.replace()来处理我可能得到的其他转义 - code> \a ,
\f
,\\\
\r
,\t
,\v
- 但不是\x
。
有没有更多的pythonic方式来做到这一点?
解决方案如果您的
winpath
是硬编码的,您可能需要使用r
在您的字符串之前表示它是一个原始字符串winpath = rC:\Users\Administrator\bin
如果
winpath
不能被硬编码,可以尝试创建一个新的字符串为:escaped_winpath =%r%winpath
(这只是
repr(winpath)
,并不会真的帮助你,因为再版(\\ bin)
是...)
解决方案是从头重建字符串:您可以在<一个href =http://code.activestate.com/recipes/65211-convert-a-string-into-a-raw-string/ =nofollow>该链接,但通用想法是:
escape_dict = {'\a':r'\a',
'\b ':r'\b',
'\c':r'\c',
'\f':r'\f',
'\ n':r'\\\
',
'\r':r'\r',
'\t':r'\t',
'\\ \\ v':r'\v',
'\'':r'\'',
'\':r'\'
def raw(text):
返回文本的原始字符串表示形式
new_string =''
用于文本中的char:
try:
new_string + = escape_dict [char]
除了KeyError:
new_string + = char
return new_string
现在, code> raw(\bin)给你
\\bin
(而不是\\x08in
)...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. This script is to handle both Unix and Windows file paths, and ultimately should run on both systems.When I run this under Unix, if I give a Windows path that inadvertently contains an escape character (e.g.
\Users\Administrator\bin
), Python will interpret the embedded\b
as the backspace character. 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.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']
.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
.Is there a more pythonic way to do this?
解决方案If your
winpath
is hard-coded, you may want to user
before your string to indicate it is a "raw string".winpath = r"C:\Users\Administrator\bin"
If
winpath
cannot be hardcoded, you can try to create a new string as:escaped_winpath = "%r" % winpath
(which is just
repr(winpath)
, and won't really help you, asrepr("\bin")
is...)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"
)...这篇关于如何防止在Python中自动转义特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!