Reputation: 39
I need to remove escape sequence. How can I do it using python?
"conv": "\"XXXXX\"",
"conv": "XXXXX",
Upvotes: 3
Views: 12604
Reputation: 385
You can use json decoder
import json
x = {"conv": "\"XXXXX\""}
x['conv'] = json.loads(x['conv']) # sets conv to "XXXXX"
Upvotes: 0
Reputation: 139
You can remove string using str.replace
method.
'\"XXXXX\"'.replace("\"","") # returns 'XXXXX'
Here's how to use str.replace()
Upvotes: -1
Reputation: 44283
You need to double-up on the back-slashes, otherwise you will not end up with something that will parse as json because otherwise you are just replacing all "
characters and leaving the backslash:
s = s.replace('\\"', '')
import json
d = {"conv": "\"XXXXX\""}
s = json.dumps(d)
print(s) # -> {"conv": "\"XXXXX\""}
s = s.replace('\\"', '')
print(s) # -> {"conv": "XXXXX"}
print(json.loads(s)) # -> {'conv': 'XXXXX'}
Upvotes: 0