Python转义符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18682695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python escape character
提问by terrachild
I've struggled for hours with this and although I found a solution, I don't like it. Is there a built in way to solve this:
我为此苦苦挣扎了几个小时,虽然我找到了解决方案,但我不喜欢它。有没有内置的方法来解决这个问题:
You are on Windows with a variable containing a path. You are trying to open a file with it, but it contains escape characters that you can't determine until runtime.
您在 Windows 上使用包含路径的变量。您试图用它打开一个文件,但它包含转义字符,直到运行时您才能确定。
If you use 'shutil' and do:
shutil.copy(file_path, new_file_path)
如果您使用 'shutil' 并执行以下操作:
shutil.copy(file_path, new_file_path)
It works fine.
它工作正常。
But if you try and use the same path with:
但是,如果您尝试使用相同的路径:
f = open(file_path, encoding="utf8")
f = open(file_path, encoding="utf8")
It doesn't work because the '\a' in the path is read as a 'Bell' = 7
它不起作用,因为路径中的 '\a' 被读取为 'Bell' = 7
I tried doing all of these, but the only thing I've gotten to work is the custom function 'reconstruct_broken_string'.
我尝试做所有这些,但我唯一能做的就是自定义函数“reconstruct_broken_string”。
file_path = "F:\ScriptsFilePath\addons\import_test.py"
print(sys.getdefaultencoding())
print()
print(file_path.replace('\', r'\'))
print( '%r' % (file_path))
print( r'r"' + "'" + file_path+ "'")
print(file_path.encode('unicode-escape'))
print(os.path.normpath(file_path))
print(repr(file_path))
print()
print(reconstruct_broken_string(file_path))
backslash_map = { '\a': r'\a', '\b': r'\b', '\f': r'\f',
'\n': r'\n', '\r': r'\r', '\t': r'\t', '\v': r'\v' }
def reconstruct_broken_string(s):
for key, value in backslash_map.items():
s = s.replace(key, value)
return s
Here is the printout:
这是打印输出:
utf-8
F:\ScriptsFilePathddons\import_test.py
'F:\ScriptsFilePath\x07ddons\import_test.py'
r"'F:\ScriptsFilePathddons\import_test.py'
b'F:\\ScriptsFilePath\x07ddons\\import_test.py'
F:\ScriptsFilePathddons\import_test.py
'F:\ScriptsFilePath\x07ddons\import_test.py'
F:\ScriptsFilePath\addons\import_test.py
Is there a built in way to do this rather than this function? Why does it work with 'shutil' and not 'open'
有没有内置的方法来做到这一点而不是这个功能?为什么它适用于“shutil”而不是“open”
Thanks
谢谢
回答by Rob?
Your problem is on this line:
你的问题在这一行:
file_path = "F:\ScriptsFilePath\addons\import_test.py"
Try one of these:
尝试以下方法之一:
file_path = r"F:\ScriptsFilePath\addons\import_test.py"
file_path = "F:\ScriptsFilePath\addons\import_test.py"
Or even:
甚至:
file_path = "F:/ScriptsFilePath/addons/import_test.py"
(Yes, Windows accept forward slash as a file separator.)
(是的,Windows 接受正斜杠作为文件分隔符。)
Ref: http://docs.python.org/2/reference/lexical_analysis.html#string-literals
参考:http: //docs.python.org/2/reference/lexical_analysis.html#string-literals
回答by Hrishi
If the path is in a variable, just replace all '\' with '/' using any of Python's string manipulation functions. It should solve the problem.
如果路径在变量中,只需使用 Python 的任何字符串操作函数将所有 '\' 替换为 '/'。它应该可以解决问题。
回答by terrachild
Here is a simplified version demonstrating how 'repr' doesn't work correctly.
这是一个简化版本,演示了“repr”如何无法正常工作。
file_path = "F:\tab\a_bell\newline.py"
print(file_path)
print(repr(file_path))
This prints:
这打印:
F: ab_bell
ewline.py
and
F:\tab\x07_bell\newline.py'
As you can see 'repr' works with escape-tab, escape-newline, etc., but it doesn't work with '\a' which is escape bell.
正如您所看到的,'repr' 可用于转义标签、转义换行符等,但它不适用于转义铃的 '\a'。
Is this is bug in 'repr'? Is there a built in solution to this problem that doesn't require programmers to write a custom function like the 'reconstruct_broken_string(s)'? If not, how can python be this lame?
这是'repr'中的错误吗?是否有针对此问题的内置解决方案,不需要程序员编写像“reconstruct_broken_string(s)”这样的自定义函数?如果没有,python怎么会这么蹩脚?
回答by jenia
I have experienced same exact problem - tried path = 'C:\temp\importfile.xlsx' and kept getting an error "No such file or directory: 'C:\Temp\importdata.xlsx'". I used forward slashes instead and my import worked. Have you tried file_path = "F:/ScriptsFilePath/addons/import_test.py"?
我遇到了同样的问题 - 尝试 path = 'C:\temp\importfile.xlsx' 并不断收到错误“没有这样的文件或目录:'C:\Temp\importdata.xlsx'”。我改用正斜杠,我的导入工作。你试过 file_path = "F:/ScriptsFilePath/addons/import_test.py" 吗?
回答by Jasper
You can use an r in front of a string so that Python handles it as a raw string-
您可以在字符串前使用 r,以便 Python 将其作为原始字符串进行处理-
filePath = input()
try:
print(filePath)
except:
print(r""+filePath)