-3

I have test.json file having ( I cant have single backslash in the json file,let me know if any ways i can do it and I need single backslash in the output)

{"test":"value\\123"}

I need to read this content from the file and have only single backslash in the output

#load the json files    
with open("details_json.json", 'r') as j:
    contents = json.loads(j.read())

print(contents)

expected output is:

{"test":"value\123"}

I need to have single backslash in the output

KS HARSHA
  • 67
  • 2
  • 7

1 Answers1

0

Nope, that's not the expected output. Do print(contents['test']) and you'll see that the dictionary contains exactly what you need. The "print" statement is doing high-level formatting, because you are asking it to print the whole dictionary. It does that by printing the REPRESENTATION of each of the keys and values. The representation of a string with backslashes is to print it exactly as you would type it, which means backslashes get double.

Don't be fooled between what the variables contain and how they are displayed.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • 1
    All this text and not explaining what is *actually* going on? (the word "escape" does not appear once) – DeepSpace May 05 '21 at 22:12