在python中将反斜杠转换为正斜杠
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4297450/
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
Convert backward slash to forward slash in python
提问by abhishek
Hi I have read articles related converting backward to forward slashes. But sol was to use raw string.
嗨,我读过与向后转换为正斜杠相关的文章。但是 sol 是使用原始字符串。
But Problem in my case is :
但就我而言,问题是:
I will get file path dynamically to a variable var='C:\dummy_folder\a.txt' In this case i need to convert it to Forward slashes. But due to '\a',i am not able to convert to forward slashes
我将动态获取变量 var='C:\dummy_folder\a.txt' 的文件路径在这种情况下,我需要将其转换为正斜杠。但由于 '\a',我无法转换为正斜杠
How to i convert it? OR How should i change this string to raw string so that i can change it to forward slash
我如何转换它?或者我应该如何将此字符串更改为原始字符串,以便我可以将其更改为正斜杠
采纳答案by user225312
Don't do this. Just use os.pathand let it handle everything. You should not explicitly set the forward or backward slashes.
不要这样做。只需使用os.path并让它处理一切。您不应显式设置正斜杠或反斜杠。
>>> var=r'C:\dummy_folder\a.txt'
>>> var.replace('\', '/')
'C:/dummy_folder/a.txt'
But again, don't. Just use os.path and be happy!
但是,再次,不要。只需使用 os.path 并快乐!
回答by moinudin
>>> 'C:\dummy_folder\a.txt'.replace('\', '/')
'C:/dummy_folder/a.txt'
In a string literal, you need to escape the \character.
在字符串文字中,您需要对\字符进行转义。
回答by Cameron
Raw strings are for string literals (written directly in the source file), which doesn't seem to be the case here. In any case, forward slashes are not special characters -- they can be embedded in a regular string without problems. It's backslashes that normally have other meaning in a string, and need to be "escaped" so that they get interpreted as literal backslashes.
原始字符串用于字符串文字(直接写在源文件中),这里似乎并非如此。在任何情况下,正斜杠都不是特殊字符——它们可以毫无问题地嵌入到常规字符串中。反斜杠通常在字符串中具有其他含义,需要“转义”以便将它们解释为文字反斜杠。
To replace backslashes with forward slashes:
用正斜杠替换反斜杠:
# Python:
string = r'C:\dummy_folder\a.txt'
string = string.replace('\', '/')
# Ruby:
string = 'C:\dummy_folder\a.txt'
string = string.gsub('\', '/')
回答by lbz
Handling paths as a mere string could put you into troubles.; even more if the path you are handling is an user input or may vary in unpredictable ways.
将路径作为单纯的字符串处理可能会给您带来麻烦。如果您处理的路径是用户输入或可能以不可预测的方式变化,则更是如此。
Different OS have different way to express the path of a given file, and every modern programming language has own methods to handle paths and file system references. Surely Python and Ruby have it:
不同的操作系统有不同的方式来表示给定文件的路径,每种现代编程语言都有自己的方法来处理路径和文件系统引用。Python 和 Ruby 肯定有:
If you really need to handle strings:
如果你真的需要处理字符串:
- Python: string.replace
- Ruby : string.gsub
- Python:string.replace
- 红宝石:string.gsub
回答by hopia
There is also os.path.normpath(), which converts backslashes and slashes depending on the local OS. Please see herefor detailed usage info. You would use it this way:
还有os.path.normpath(),它根据本地操作系统转换反斜杠和斜杠。请参阅此处了解详细的使用信息。你会这样使用它:
>>> string = r'C:/dummy_folder/a.txt'
>>> os.path.normpath(string)
'C:\dummy_folder\a.txt'

