Python 将 \\ 替换为 \

发布于 2024-10-20 08:17:25 字数 1094 浏览 6 评论 0原文

所以我似乎无法弄清楚这一点...我有一个字符串,“a\\nb”,我希望它变成“a\nb” 。我已经尝试了以下所有方法,但似乎都不起作用;

>>> a
'a\\nb'
>>> a.replace("\\","\")
  File "<stdin>", line 1
    a.replace("\\","\")
                      ^
SyntaxError: EOL while scanning string literal
>>> a.replace("\\",r"\")
  File "<stdin>", line 1
    a.replace("\\",r"\")
                       ^
SyntaxError: EOL while scanning string literal
>>> a.replace("\\",r"\\")
'a\\\\nb'
>>> a.replace("\\","\\")
'a\\nb'

我真的不明白为什么最后一个有效,因为这个工作正常:

>>> a.replace("\\","%")
'a%nb'

我在这里缺少什么吗?

编辑 我知道 \ 是一个转义字符。我在这里想做的是将所有 \\n \\t 等转换为 \n \t 等,并且替换似乎没有按照我想象的方式工作。

>>> a = "a\\nb"
>>> b = "a\nb"
>>> print a
a\nb
>>> print b
a
b
>>> a.replace("\\","\\")
'a\\nb'
>>> a.replace("\\\\","\\")
'a\\nb'

我希望字符串 a 看起来像字符串 b。但替换并不像我想象的那样替换斜杠。

So I can't seem to figure this out... I have a string say, "a\\nb" and I want this to become "a\nb". I've tried all the following and none seem to work;

>>> a
'a\\nb'
>>> a.replace("\\","\")
  File "<stdin>", line 1
    a.replace("\\","\")
                      ^
SyntaxError: EOL while scanning string literal
>>> a.replace("\\",r"\")
  File "<stdin>", line 1
    a.replace("\\",r"\")
                       ^
SyntaxError: EOL while scanning string literal
>>> a.replace("\\",r"\\")
'a\\\\nb'
>>> a.replace("\\","\\")
'a\\nb'

I really don't understand why the last one works, because this works fine:

>>> a.replace("\\","%")
'a%nb'

Is there something I'm missing here?

EDIT I understand that \ is an escape character. What I'm trying to do here is turn all \\n \\t etc. into \n \t etc. and replace doesn't seem to be working the way I imagined it would.

>>> a = "a\\nb"
>>> b = "a\nb"
>>> print a
a\nb
>>> print b
a
b
>>> a.replace("\\","\\")
'a\\nb'
>>> a.replace("\\\\","\\")
'a\\nb'

I want string a to look like string b. But replace isn't replacing slashes like I thought it would.

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

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

发布评论

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

评论(9

无言温柔 2024-10-27 08:17:25

无需为此使用替换。

您拥有的是一个编码字符串(使用 string_escape 编码)并且您想要对其进行解码:

>>> s = r"Escaped\nNewline"
>>> print s
Escaped\nNewline
>>> s.decode('string_escape')
'Escaped\nNewline'
>>> print s.decode('string_escape')
Escaped
Newline
>>> "a\\nb".decode('string_escape')
'a\nb'

在 Python 3 中:

>>> import codecs
>>> codecs.decode('\\n\\x21', 'unicode_escape')
'\n!'

There's no need to use replace for this.

What you have is a encoded string (using the string_escape encoding) and you want to decode it:

>>> s = r"Escaped\nNewline"
>>> print s
Escaped\nNewline
>>> s.decode('string_escape')
'Escaped\nNewline'
>>> print s.decode('string_escape')
Escaped
Newline
>>> "a\\nb".decode('string_escape')
'a\nb'

In Python 3:

>>> import codecs
>>> codecs.decode('\\n\\x21', 'unicode_escape')
'\n!'
相权↑美人 2024-10-27 08:17:25

你错过了, \ 是转义字符。

看这里:http://docs.python.org/reference/lexical_analysis.html
2.4.1“转义序列”

最重要的是 \n 是换行符。
并且 \\ 是转义转义字符 :D

>>> a = 'a\\\\nb'
>>> a
'a\\\\nb'
>>> print a
a\\nb
>>> a.replace('\\\\', '\\')
'a\\nb'
>>> print a.replace('\\\\', '\\')
a\nb

You are missing, that \ is the escape character.

Look here: http://docs.python.org/reference/lexical_analysis.html
at 2.4.1 "Escape Sequence"

Most importantly \n is a newline character.
And \\ is an escaped escape character :D

>>> a = 'a\\\\nb'
>>> a
'a\\\\nb'
>>> print a
a\\nb
>>> a.replace('\\\\', '\\')
'a\\nb'
>>> print a.replace('\\\\', '\\')
a\nb
探春 2024-10-27 08:17:25
r'a\\nb'.replace('\\\\', '\\')

或者

'a\nb'.replace('\n', '\\n')
r'a\\nb'.replace('\\\\', '\\')

or

'a\nb'.replace('\n', '\\n')
So尛奶瓶 2024-10-27 08:17:25

您的原始字符串 a = 'a\\nb' 实际上没有两个 '\' 字符,第一个字符是后者的转义字符。如果你这样做,打印一个,你会发现实际上只有一个'\'字符。

>>> a = 'a\\nb'
>>> print a
a\nb

但是,如果您的意思是将 '\n' 解释为换行符,而不转义斜杠,则:

>>> b = a.replace('\\n', '\n')
>>> b
'a\nb'
>>> print b
a
b

Your original string, a = 'a\\nb' does not actually have two '\' characters, the first one is an escape for the latter. If you do, print a, you'll see that you actually have only one '\' character.

>>> a = 'a\\nb'
>>> print a
a\nb

If, however, what you mean is to interpret the '\n' as a newline character, without escaping the slash, then:

>>> b = a.replace('\\n', '\n')
>>> b
'a\nb'
>>> print b
a
b
谁把谁当真 2024-10-27 08:17:25

这是因为,即使在“原始”字符串(=在起始引号之前带有 r 的字符串)中,未转义的转义字符也不能是字符串中的最后一个字符。这应该有效:

'\\ '[0]

It's because, even in "raw" strings (=strings with an r before the starting quote(s)), an unescaped escape character cannot be the last character in the string. This should work instead:

'\\ '[0]
迷离° 2024-10-27 08:17:25

在 Python 字符串文字中,反斜杠是转义字符。当交互式提示显示字符串的值时也是如此。它将为您提供字符串的文字代码表示形式。使用 print 语句查看字符串的实际情况。

这个例子显示了差异:

>>> '\\'
'\\'
>>> print '\\'
\

In Python string literals, backslash is an escape character. This is also true when the interactive prompt shows you the value of a string. It will give you the literal code representation of the string. Use the print statement to see what the string actually looks like.

This example shows the difference:

>>> '\\'
'\\'
>>> print '\\'
\
诠释孤独 2024-10-27 08:17:25

在 Python 3 中它将是:

bytes(s, 'utf-8').decode("unicode_escape")

In Python 3 it will be:

bytes(s, 'utf-8').decode("unicode_escape")
故人的歌 2024-10-27 08:17:25

这适用于使用 Python 3.x 的 Windows:

import os
str(filepath).replace(os.path.sep, '/')

其中:os.path.sep 在 Windows 上是 \,在 Linux 上是 /

案例研究

使用它来防止生成 Markdown 文件然后将其渲染为 pdf 时出现错误。

This works on Windows with Python 3.x:

import os
str(filepath).replace(os.path.sep, '/')

Where: os.path.sep is \ on Windows and / on Linux.

Case study

Used this to prevent errors when generating a Markdown file then rendering it to pdf.

野鹿林 2024-10-27 08:17:25
path = "C:\\Users\\Programming\\Downloads"
# Replace \\ with a \ along with any random key multiple times
path.replace('\\', '\pppyyyttthhhooonnn')
# Now replace pppyyyttthhhooonnn with a blank string
path.replace("pppyyyttthhhooonnn", "")

print(path)

#输出...
C:\用户\编程\下载

path = "C:\\Users\\Programming\\Downloads"
# Replace \\ with a \ along with any random key multiple times
path.replace('\\', '\pppyyyttthhhooonnn')
# Now replace pppyyyttthhhooonnn with a blank string
path.replace("pppyyyttthhhooonnn", "")

print(path)

#Output...
C:\Users\Programming\Downloads

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