Unable To Remove Additional Double Quotes In Json String Added By Python
Solution 1:
If the files as stored are intended to be JSON then they are invalid. The JSON format doesn't allow the use of single quotes to delimit strings. Assuming you have no single quotes within the key/value strings themselves, you can replace the single quotes with double quotes and then read in using the JSON module:
import json
x = "{'FileID': 'a3333.txt','Timestamp': '2014-12-05T02:01:28.271Z','SuccessList':'a,b,c,d,e'}"
x = x.replace("'", '"')
j = json.loads(x)
print j
yields:
{'FileID': 'a3333.txt','Timestamp': '2014-12-05T02:01:28.271Z','SuccessList':'a,b,c,d,e'}
Alternatively:
If the data is the string representation of a Python dict
, you can read it in with eval
. Using eval
is dangerous (see Ned Batchelder's thoughts on it). That said, if you wrote the file yourself and you are confident that it contains no malicious code, you can use eval
to read the string as Python source code:
x = "{'FileID': 'a3333.txt','Timestamp': '2014-12-05T02:01:28.271Z','SuccessList':'a,b,c,d,e'}"eval(x, {'__builtins__': {}})
yields:
{'FileID': 'a3333.txt','Timestamp': '2014-12-05T02:01:28.271Z','SuccessList':'a,b,c,d,e'}
Don't make a habit of this though! The right way to do this is to save the data to a file in a proper serialization format and then to read it from disk using a library like the json
module.
Solution 2:
If your string actually contains double quotes (which it might not, as they could just be part of the printed representation), you could get rid of them with a slice, e.g.,
>>>hello = '"hello more stuff things"'>>>hello
'"hello more stuff things"'
>>>hello[1:-1]
'hello more stuff things'
Note in this case that the outer single quotes are not part of the string, they are just part of the printed representation.
Solution 3:
You can convert the string
back to dictionary
using
import re
x="{'FileID': 'a3333.txt','Timestamp': '2014-12-05T02:01:28.271Z','SuccessList':'a,b,c,d,e'}"printdict(re.findall(r"""'([^']*)'\s*:\s*'([^']*)'""",x))
Solution 4:
The double quotes you are referring to are not part of the string, and are just there to delimit it.
If you assign the string "thi's'" to a variable:
>>> a = "thi's'"
the first element in that string is t
:
>>> a[0]
t
In your example, the first element in the string would be {
, which I believe is what you would expect.
Post a Comment for "Unable To Remove Additional Double Quotes In Json String Added By Python"