だから私はこれを理解できないようです...私は文字列を言っており、"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
etc. を etc. に変換\n
\t
することです。replace は、私が想像していたようには機能していないようです。
>>> a = "a\\nb"
>>> b = "a\nb"
>>> print a
a\nb
>>> print b
a
b
>>> a.replace("\\","\\")
'a\\nb'
>>> a.replace("\\\\","\\")
'a\\nb'
文字列 a を文字列 b のように見せたい。しかし、私が思っていたように、置換はスラッシュを置換していません。