Python 将 \\ 替换为 \
所以我似乎无法弄清楚这一点...我有一个字符串,“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
无需为此使用替换。
您拥有的是一个编码字符串(使用
string_escape
编码)并且您想要对其进行解码:在 Python 3 中:
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:In Python 3:
你错过了, \ 是转义字符。
看这里:http://docs.python.org/reference/lexical_analysis.html
2.4.1“转义序列”
最重要的是 \n 是换行符。
并且 \\ 是转义转义字符 :D
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
或者
or
您的原始字符串
a = 'a\\nb'
实际上没有两个'\'
字符,第一个字符是后者的转义字符。如果你这样做,打印一个
,你会发现实际上只有一个'\'
字符。但是,如果您的意思是将
'\n'
解释为换行符,而不转义斜杠,则: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.If, however, what you mean is to interpret the
'\n'
as a newline character, without escaping the slash, then:这是因为,即使在“原始”字符串(=在起始引号之前带有
r
的字符串)中,未转义的转义字符也不能是字符串中的最后一个字符。这应该有效: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:在 Python 字符串文字中,反斜杠是转义字符。当交互式提示显示字符串的值时也是如此。它将为您提供字符串的文字代码表示形式。使用
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:
在 Python 3 中它将是:
In Python 3 it will be:
这适用于使用 Python 3.x 的 Windows:
其中:
os.path.sep
在 Windows 上是\
,在 Linux 上是/
。案例研究
使用它来防止生成 Markdown 文件然后将其渲染为
pdf
时出现错误。This works on Windows with Python 3.x:
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
.#输出...
C:\用户\编程\下载
#Output...
C:\Users\Programming\Downloads