Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python替换\\为\_Python_String_Replace_Double_Slash - Fatal编程技术网

Python替换\\为\

Python替换\\为\,python,string,replace,double,slash,Python,String,Replace,Double,Slash,所以我似乎无法理解这一点。。。我有一个字符串说,“a\\nb”,我希望它变成“a\nb”。我尝试了以下所有方法,但没有一种有效 >>> a 'a\\nb' >>> a.replace("\\","\") File "<stdin>", line 1 a.replace("\\","\") ^ SyntaxError: EOL while scanning string literal >

所以我似乎无法理解这一点。。。我有一个字符串说,
“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'
这里有我遗漏的东西吗

编辑我知道\是转义字符。我在这里试图做的是将所有
\\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。但是replace并没有像我想象的那样替换斜杠。

您丢失了,这\是转义字符

>>> a = "a\\nb"
>>> b = "a\nb"
>>> print a
a\nb
>>> print b
a
b
>>> a.replace("\\","\\")
'a\\nb'
>>> a.replace("\\\\","\\")
'a\\nb'
>>> a = 'a\\nb'
>>> print a
a\nb
看这里: 第2.4.1节“逃逸顺序”

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

>>> a = 'a\\\\nb'
>>> a
'a\\\\nb'
>>> print a
a\\nb
>>> a.replace('\\\\', '\\')
'a\\nb'
>>> print a.replace('\\\\', '\\')
a\nb
这是因为,即使在“原始”字符串(=在起始引号之前带有
r
的字符串)中,未转义的转义字符也不能是字符串中的最后一个字符。这应该起作用:

'\\ '[0]

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

此示例显示了不同之处:

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


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

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

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

没有必要为此使用“替换”

您拥有的是一个编码字符串(使用
string\u 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!'
#输出。。。
C:\Users\Programming\Downloads

您的原始字符串,
a='a\\nb'
实际上没有两个
'\'
字符,第一个字符是后者的转义符。如果您这样做,
打印一个
,您将看到实际上只有一个
“\'
字符。这很有效,谢谢!问题的一部分是,我认为\\n是3个字符而不是2个字符,这让我感到困惑。我认为Python 3的等价物是:
字节(s,'utf-8')。decode(“unicode_escape”)
或者在Python 3中:
'a\\nb'.encode()。decode(“unicode_escape”)
@JonathanHartley您应该将此作为单独的答案提交。不,您应该!
>>> import codecs
>>> codecs.decode('\\n\\x21', 'unicode_escape')
'\n!'
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)