-5

So lets say I receive a string of python code that looks as follows:

"def fib(num):\\n\\t#insert code here\\n\\tnumbers = [1, 2, 3]\\n\\tfor n in numbers:\\n\\t    print(n + \\"\\\\n\\")"

I want to rewrite this string so that every time there is two backslashes, they get replaced by one backslash.

I have tried using code = re.sub(r'(\\)+', "\\", code) but it gives me an error because the regex pattern ends with a backslash which is not allowed.

If I try to write code = re.sub(r'(\\)+', r'\\', code), however, it writes the backslash twice instead of once and I can't write r'\' because python won't allow it. How would I go about doing this?

Edits with more info:

I'm using sys.stderr.write(repr(code) + '\n') to find the representation of the strings

Using the above string as the input, I get the following results:

Method One

code = re.sub(r'(\\\\)+', r"\\", code)

Yields: 'def fib(num):\\n\\t#insert code here\\n\\tnumbers = [1, 2, 3]\\n\\tfor n in numbers:\\n\\t print(n + \\"\\n\\")'

And writes to file: \n\t#insert code here\n\tnumbers = [1, 2, 3]\n\tfor n in numbers:\n\t print(n + \"\n\")

Method Two

code = code.replace(r'\\', '\\')

Yields: 'def fib(num):\\n\\t#insert code here\\n\\tnumbers = [1, 2, 3]\\n\\tfor n in numbers:\\n\\t print(n + \\"\\n\\")'

And writes to file: \n\t#insert code here\n\tnumbers = [1, 2, 3]\n\tfor n in numbers:\n\t print(n + \"\n\")

Method Three

code = re.sub(r'(\\)+', "\\", code)

Yields Error: sre_constants.error: bad escape (end of pattern) at position 0

Method Four

code = re.sub(r'(\\)+', r'\\', code)

Yields: 'def fib(num):\\n\\t#insert code here\\n\\tnumbers = [1, 2, 3]\\n\\tfor n in numbers:\\n\\t print(n + \\"\\n\\")'

And writes to file: \n\t#insert code here\n\tnumbers = [1, 2, 3]\n\tfor n in numbers:\n\t print(n + \"\n\")

Community
  • 1
  • 1
mattbeiswenger
  • 383
  • 3
  • 11
  • `r'\\'` is correct, there is no issue in the question, closing as typo. – Wiktor Stribiżew Feb 06 '18 at 22:21
  • I get "SyntaxError: EOL while scanning string literal" because the backslash is escaping the rawstring quote – mattbeiswenger Feb 06 '18 at 22:22
  • 1
    @usr2564301 No, there is only 1 literal backslash.OP sees the console output. – Wiktor Stribiżew Feb 06 '18 at 22:23
  • Matt, we need to know for sure whether that string that *looks* like what you show *is* the actual string. Be careful not to mix what Python shows with what the string actually contains. – Jongware Feb 06 '18 at 22:28
  • @usr2564301 I used sys.stderr.write(repr(code) + '\n') to find the above string – mattbeiswenger Feb 06 '18 at 22:31
  • And what appears if you use `print (code)`? – Jongware Feb 06 '18 at 22:33
  • As suspected, you don't have two backslashes in a row anywhere in your string. – Josh Lee Feb 07 '18 at 01:22
  • https://stackoverflow.com/questions/1885181/how-do-i-un-escape-a-backslash-escaped-string-in-python – Josh Lee Feb 07 '18 at 01:23
  • @JoshLee I apologize for not posing the question correctly, I think I was a bit confused with when a backslash is considered "in the string" or not. The link to the other SO question was a big help and allowed me to solve the problem. I should've known python had a built-in way of dealing with this. Thanks! – mattbeiswenger Feb 07 '18 at 01:55
  • @Matt Makes sense, there's a surprising disconnect between "The computer showed this to me" and what's actually in the string, and there's specialized knowledge in asking the right question. Fortunately, the right question has already been asked and answered well. :) – Josh Lee Feb 07 '18 at 12:31

1 Answers1

0

Don't use re as it (apparently!) has all kinds of problems with backslashes. You can use the standard replace function:

c = code.replace(r'\\','\\')

– note the r before the first string but not before the second string.

This replaces the single occurrence of \\ in your original string (there are 12 backslashes in there, but only one pair):

code = 'def fib(num):\\n\\t#insert code here\\n\\tnumbers = [1, 2, 3]\\n\\tfor n in numbers:\\n\\t    print(n + \\"\\\\n\\")'
c = code.replace(r'\\','\\')
print (c)

with the result

def fib(num):\n\t#insert code here\n\tnumbers = [1, 2, 3]\n\tfor n in numbers:\n\t    print(n + \"\n\")

Other representations of this result may still show double backslashes, but that's just what Python does:

>> c
'def fib(num):\\n\\t#insert code here\\n\\tnumbers = [1, 2, 3]\\n\\tfor n in numbers:\\n\\t    print(n + \\"\\n\\")'
>>> sys.stderr.write(repr(c)+'\n')
'def fib(num):\\n\\t#insert code here\\n\\tnumbers = [1, 2, 3]\\n\\tfor n in numbers:\\n\\t    print(n + \\"\\n\\")'
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Jongware
  • 22,200
  • 8
  • 54
  • 100