Python将反斜杠转换为正斜杠

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25146960/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:48:01  来源:igfitidea点击:

Python Convert Back Slashes to forward slashes

pythonreplace

提问by John87

I am working in python and I need to convert this:

我在 python 中工作,我需要转换它:

C:\folderA\folderB to C:/folderA/folderB

C:\folderA\folderB 到 C:/folderA/folderB

I have three approaches:

我有三种方法:

dir = s.replace('\','/')

dir = os.path.normpath(s) 

dir = os.path.normcase(s)

In each scenario the output has been

在每种情况下,输出都是

C:folderAfolderB

C:folderAfolderB

I'm not sure what I am doing wrong, any suggestions?

我不确定我做错了什么,有什么建议吗?

采纳答案by Jason S

Your specific problem is the order and escaping of your replacearguments, should be

你的具体问题是你的replace论点的顺序和转义,应该是

s.replace('\', '/')

Then there's:

然后是:

posixpath.join(*s.split('\'))

Which on a *nix platform is equivalent to:

在 *nix 平台上相当于:

os.path.join(*s.split('\'))

But don't rely on that on Windows because it will prefer the platform-specific separator. Also:

但是不要在 Windows 上依赖它,因为它会更喜欢特定于平台的分隔符。还:

Note that on Windows, since there is a current directory for each drive, os.path.join("c:", "foo") represents a path relative to the current directory on drive C: (c:foo), not c:\foo.

请注意,在 Windows 上,由于每个驱动器都有一个当前目录,因此 os.path.join("c:", "foo") 表示相对于驱动器 C: (c:foo) 上的当前目录的路径,而不是 c :\foo.

回答by TheoretiCAL

Try

尝试

path = '/'.join(path.split('\'))

回答by T P Saravanan

How about :

怎么样 :

import ntpath
import posixpath
.
.
.
dir = posixpath.join(*ntpath.split(s))
.
.

回答by Mohammad ElNesr

To define the path's variable you have to add rinitially, then add the replace statement .replace('\\', '/')at the end.

要定义路径的变量,您必须首先添加r,然后.replace('\\', '/')在最后添加替换语句。

for example:

例如:

In>>  path2 = r'C:\Users\User\Documents\Project\Em2Lph\'.replace('\', '/')
In>>  path2 
Out>> 'C:/Users/User/Documents/Project/Em2Lph/'

This solution requires no additional libraries

此解决方案不需要额外的库

回答by Numabyte

I recently found this and thought worth sharing:

我最近发现了这个并认为值得分享:

import os

path = "C:\temp\myFolder\example\"

newPath = path.replace(os.sep, '/')

print newPath


Output:<< C:/temp/myFolder/example/  >>

回答by scapa

Path names are formatted di?erently in Windows. the solution is simple, suppose you have a path string like this:

路径名在 Windows 中的格式不同。解决方案很简单,假设你有一个这样的路径字符串:

data_file = "/Users/username/Downloads/PMLSdata/series.csv"

simply you have to change it to this: (adding r front of the path)

只需将其更改为:(在路径前添加 r )

data_file = r"/Users/username/Downloads/PMLSdata/series.csv"

The modi?er r before the string tells Python that this is a raw string. In raw strings, the backslash is interpreted literally, not as an escape character.

字符串前的修饰符 r 告诉 Python 这是一个原始字符串。在原始字符串中,反斜杠按字面解释,而不是转义字符。