Python Replace \\ with \

128,201

Solution 1

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!'

Solution 2

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

Solution 3

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

or

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

Solution 4

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

Solution 5

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]
Share:
128,201
kand
Author by

kand

Updated on July 09, 2022

Comments

  • kand
    kand almost 2 years

    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.

  • kand
    kand about 13 years
    This worked thanks! Part of the issue was that I was confusing myself by thinking that \\n was 3 characters instead of just 2.
  • Jonathan Hartley
    Jonathan Hartley about 13 years
    I think the equivalent of this for Python 3 is: bytes(s, 'utf-8').decode("unicode_escape")
  • Mike Chamberlain
    Mike Chamberlain about 12 years
    Alternatively in Python 3: 'a\\nb'.encode().decode('unicode_escape')
  • Luke Davis
    Luke Davis over 6 years
    @JonathanHartley you should submit this as a separate answer.
  • Jonathan Hartley
    Jonathan Hartley over 6 years
    No, you should!
  • born_naked
    born_naked almost 3 years
    This won't remove the double slash in all cases, for instance \\N will fail. print(codecs.decode('\\N\\x21', 'unicode_escape')) w/error message "malformed \N character escape. "