1

many of the findings are for python2, my case is python3:

I need to replace double back slash to single back slash

a = 'RXIE-SERVER\\MSSQLSERVER_NEW'

An "accepted answer" from this post python3 replacing double backslash with single backslash doesn't work here:

enter image description here

So what is the problem here? Thank you very much.

[UPDATE]:

Thanks to @ShadowRanger's reply, so if print the string after the replace operation, it is working expected, however, in my case I need to concat the post-replacement to generate another variable (conn_str), as you can see, it is still showing double backslash.

Any clue for a workaround?

Thank you very much.

server = str.replace(data['server'], "\\\\", "\\")
database = data['database']
driver = data['driver']
auth = data['auth']

conn_str = "DRIVER={" + driver + "}; SERVER=" + server + "; Database=" + database + ";" + auth

enter image description here

mdivk
  • 3,545
  • 8
  • 53
  • 91
  • Similar question, not quite a duplicate: [How to use replace double backslashes to single one for byte string in Python](https://stackoverflow.com/q/51210485/4518341) – wjandrea Feb 26 '20 at 21:42
  • Thanks. That post was very confusing, not well explained to me. – mdivk Feb 27 '20 at 03:51

1 Answers1

3
a = 'RXIE-SERVER\\MSSQLSERVER_NEW'

doesn't have a double backslash. It has an escaped single backslash, it's just safer (and will eventually be required) to escape it so Python doesn't think \M is an attempt at a string escape. If you do:

print(a)

you'll see it only prints one backslash (because print outputs the raw data without showing escapes).

The reason a.replace('\\', '') doesn't work is because it replaced the single backslash with nothing (and it would do so for all backslashes); a.replace('\\\\', '\\') doesn't work because '\\\\' represents the actual doubled backslash, and you don't have any of those.

If your input came from some other source (not the literal you described) and actually has a doubled-backslash, then a.replace('\\\\', '\\') actually worked, but REPL's echo the repr of the object, and for str, that means adding the backslash escape to make it a legal, equivalent str literal, so it looked like a double-backslash, but only had one. If you change >>> a.replace('\\\\', '\\') to >>> print(a.replace('\\\\', '\\')) (which prints the human-friendly form, not the repr), you'll see it display only a single backslash.

If you don't like how it looks in your code, use raw strings to remove the need for the escape:

a = r'RXIE-SERVER\MSSQLSERVER_NEW'
#   ^ note prefix that makes it raw
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • Thank you, but sorry the original string is generated from somewhere else, I do need to find a way to replace the double backslash into single backslash. – mdivk Feb 27 '20 at 03:48
  • @mdivk: If it actually has a double-backslash, then the `a.replace('\\\\', '\\')` code actually worked. You just didn't think it did, because the REPL's echoing functionality echoes the `repr` of the object, which would display the resulting single backslash as a double backslash (because it's intended to be a legal `str` literal, which would need the extra backslash as an escape). Change `>>> a.replace('\\\\', '\\')` to `>>> print(a.replace('\\\\', '\\'))` and you'll see it's really only one backslash. – ShadowRanger Feb 27 '20 at 04:00
  • Thank you @ShadowRanger, yes, I thought it was not done, but actually it did. I've made some update to my OP, still not what needed. – mdivk Feb 27 '20 at 13:38
  • @mdivk: Yes, it is what you needed. You're looking at the `repr` again; Python doesn't magically add a slash back in. Just use it, you'll find it behaves exactly as you expect it to. – ShadowRanger Feb 28 '20 at 01:06
  • No, it doesn't. I will end up of a connection string that contains a non-recognized server `RXIE-SERVER\\MSSQLSERVER_NEW`, and I have to manually create a connection string with `RXIE-SERVER\MSSQLSERVER_NEW` , thanks. – mdivk Feb 28 '20 at 02:33
  • @mdivk: You performed the replace. It's done. If `print(server)` shows one backslash, so will `print(conn_str)`. – ShadowRanger Feb 28 '20 at 02:53
  • Thank you @ShadowRanger. – mdivk Feb 28 '20 at 23:08