python 将反斜杠替换为斜杠

发布于 2024-11-14 10:02:33 字数 146 浏览 5 评论 0 原文

如何转义字符串中的反斜杠:'pictures\12761_1.jpg'

我知道原始字符串。例如,如果我从 xml 文件中获取 'pictures\12761_1.jpg' 值,如何将 str 转换为 raw?

How can I escape the backslashes in the string: 'pictures\12761_1.jpg'?

I know about raw string. How can I convert str to raw if I take 'pictures\12761_1.jpg' value from xml file for example?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情深缘浅 2024-11-21 10:02:33

您可以将字符串 .replace() 方法与 rawstring 一起使用。

Python 2:

>>> print r'pictures\12761_1.jpg'.replace("\\", "/")
pictures/12761_1.jpg

Python 3:

>>> print(r'pictures\12761_1.jpg'.replace("\\", "/"))
pictures/12761_1.jpg

这里有两件事需要注意:

  • 首先,通过将 r 放在前面,将文本作为绘图字符串读取。
    细绳。如果您不提供该信息,则会出现 Unicode 错误。
  • 而且替换方法的第一个参数内给出了两个反斜杠。原因是反斜杠是与其他字母一起用作转义序列的文字。现在您可能想知道什么是转义序列。因此,转义序列是在字符串文字或字符中使用时不代表自身的字符序列。它由两个或多个以反斜杠开头的字符组成。就像 '\n' 代表换行符一样,类似地还有很多。因此,为了转义反斜杠本身(通常是转义序列的开始),我们使用另一个反斜杠来转义它。

我知道第二部分有点令人困惑,但我希望它有意义。

You can use the string .replace() method along with rawstring.

Python 2:

>>> print r'pictures\12761_1.jpg'.replace("\\", "/")
pictures/12761_1.jpg

Python 3:

>>> print(r'pictures\12761_1.jpg'.replace("\\", "/"))
pictures/12761_1.jpg

There are two things to notice here:

  • Firstly to read the text as a drawstring by putting r before the
    string. If you don't give that, there will be a Unicode error here.
  • And also that there were two backslashes given inside the replace method's first argument. The reason for that is that backslash is a literal used with other letters to work as an escape sequence. Now you might wonder what is an escape sequence. So an escape sequence is a sequence of characters that doesn't represent itself when used inside string literal or character. It is composed of two or more characters starting with a backslash. Like '\n' represents a newline and similarly there are many. So to escape backslash itself which is usually an initiation of an escape sequence, we use another backslash to escape it.

I know the second part is bit confusing but I hope it made some sense.

狂之美人 2024-11-21 10:02:33

您还可以使用拆分/连接:

print "/".join(r'pictures\12761_1.jpg'.split("\\"))

编辑:

您可以使用的另一种方法是在检索数据期间准备数据(例如,想法是在分配给变量之前更新字符串) - 例如:

f = open('c:\\tst.txt', "r")
print f.readline().replace('\\','/')

>>>'pictures/12761_1.jpg\n'

You can also use split/join:

print "/".join(r'pictures\12761_1.jpg'.split("\\"))

EDITED:

The other way you may use is to prepare data during it's retrieving(e.g. the idea is to update string before assign to variable) - for example:

f = open('c:\\tst.txt', "r")
print f.readline().replace('\\','/')

>>>'pictures/12761_1.jpg\n'
魔法唧唧 2024-11-21 10:02:33

我知道这不是你所要求的,但我认为这会更好。
最好只拥有目录的名称并使用 os.path.join(directory,filename)

"os.path.join(path, *paths)
智能地加入一个或多个路径组件。返回值是 path 和 *paths 的任何成员的串联,每个非空部分(最后一个部分除外)后面只有一个目录分隔符 (os.sep),这意味着如果最后一部分为空,结果只会以分隔符结尾。如果组件是绝对路径,则所有先前的组件都会被丢弃,并从绝对路径组件继续连接”

https://docs.python.org/2/library/os.path.html

I know it is not what you asked exactly, but I think this will work better.
Tit's better to just have the names of your directories and use os.path.join(directory,filename)

"os.path.join(path, *paths)
Join one or more path components intelligently. The return value is the concatenation of path and any members of *paths with exactly one directory separator (os.sep) following each non-empty part except the last, meaning that the result will only end in a separator if the last part is empty. If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component"

https://docs.python.org/2/library/os.path.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文