-2

I want to replace / with a \/ in a string. For an example

original_string="https://stackoverflow.com/questions/"
modified_string="https:\/\/stackoverflow.com\/questions\/"

modified_string is the required output. I tried the following and neither of them seems to be working.

modified_string=original_string.replace('/','\/')
modified_string=original_string.replace('/',r'\/')
modified_string=re.sub("/", r"\/", original_string)

They provide the following output when saving it to a file,

modified_string="https:\\/\\/stackoverflow.com\\/questions\\/"

print(modified_string) outputs the correct string ignoring the escape character, but how can we keep the same output when saving it to a file. Is there a way to disable escape characters in python and treat it as just a character?

***A Complete Sample code is added below to regenerate the problem ***

original_string="https://stackoverflow.com/questions/"
modified_string=original_string.replace('/','\/')


#url.json file contains the following
#{"website":{"title":"stackoverflow","url":"https:\/\/www.google.com\/"}}

import json
f = open('url.json')
data = json.load(f)
f.close()
data["website"]["url"]=modified_string
with open("url.json", "w") as outfile:
    json.dump(data, outfile)

#Output file comes as the following which is not the expected output
    # {"website": {"title": "stackoverflow", "url": "https:\\/\\/stackoverflow.com\\/questions\\/"}}
nrnw
  • 441
  • 6
  • 13
  • 1
    What `can we keep output inside a variable` means? Your code works fine for me in all the cases. – Marcin Orlowski May 16 '23 at 16:01
  • It is a `repr` problem or it is related to the computation? It's not clear. – Glauco May 16 '23 at 16:11
  • modified_string="https:\\/\\/stackoverflow.com\\/questions\\/" If I save this to a file (ex: using json.dump()) it will also save the extra "\" which I want to avoid. – nrnw May 16 '23 at 16:13
  • `\/` inside a string literal is the same as just `/`. So there's no difference between `original_string` and `modified_string`. – Barmar May 16 '23 at 16:39
  • 1
    Backslash has two functions in a string. It either starts an escape sequence (e.g. `\n` for newline) or it makes the following character literal. Since `\/` isn't an escape sequence, it just makes the `/` literal. – Barmar May 16 '23 at 16:41
  • If `original_string="https://stackoverflow.com/questions/"`; `modified_string="https:\/\/stackoverflow.com\/questions\/"` then `original_string.replace('/','\/') == modified_string` gives `True` so where is a problem? – JosefZ May 16 '23 at 18:02
  • To make thing easier to understand, you can link the format encoding you want to achieve (so I assume an escaping rule). Also because rules are for sure more complex (very often we want to convert in one direction and back without losing information, in your question we may lose information if there were already some `\`). But so, it helps also you to understand which level you are discussing (encoding, representation, escaping). (URL escaping? no, `/` is allowed. Shell escaping? also `/` is allowed. Or it is to distinguish `/` as *normal character* and `/` as separator? – Giacomo Catenazzi May 17 '23 at 06:14
  • 1
    Let's maybe start at the beginning and have you explain what exactly you're trying to do here. There's almost never a reason to manually futz with escaping specific characters. Why are you trying to escape slashes here? – deceze May 17 '23 at 08:23
  • This seems like an [XY Problem](https://en.wikipedia.org/wiki/XY_problem) anyway. The slash is not a metacharacter to Python or to regular expressions or to most other places where you could use a string; why do you think you want to backslash the slashes? – tripleee May 17 '23 at 08:33
  • 1
    To put it bluntly: you're confused about what you're looking at, at which point you're seeing an escaped backslash and where you're seeing a plain backslash and why. This makes me strongly suspect the original premise of trying to escape slashes is also a misunderstanding to begin with. – deceze May 17 '23 at 08:37
  • You don't want to make the backslash part of your actual string. You just want the backslash as part of the JSON-encoded representation, which means the backslash needs to be added as part of the JSON-encoding process, not before (otherwise the JSON-encoding process will escape your backslash yet again). I've added some additional duplicates above for you… – deceze May 17 '23 at 13:43
  • The code shown does not just write to a file, *it converts the data to JSON* and writes that to a file. The expansion of `\ ` to `\\ ` is done by the conversion to JSON, as you can check via `print(json.dumps(modified_string))`. Note that as far as JSON is concerned, a JSON string of `/` and `\/` are *exactly identical* so there is no need at all to manually add this escape; note that both correspond to the Python character `/`, not `\/`. – MisterMiyagi May 24 '23 at 11:25

1 Answers1

-2

orignal_string.replace seems to be doing the job for me when "manually" escaping the backslash

original_string="https://stackoverflow.com/questions/"
modified_string = original_string.replace("/" , "\\/")
print(modified_string) ## https:\/\/stackoverflow.com\/questions\/

I'm using python 3.11, and this bit of code works fine

nlouis56
  • 49
  • 6
  • Thank you for trying it out. Yes, the output is fine for the print(). But if you save the output to a file you will see escape characters also included. That is where the problem arises. I'll update the question with some sample codes. – nrnw May 17 '23 at 13:25