thirstylad
thirstylad

Reputation: 387

Unable to remove additional double quotes in json string added by python

I have a file whose contents are

{'FileID': 'a3333.txt','Timestamp': '2014-12-05T02:01:28.271Z','SuccessList':'a,b,c,d,e'}

When I read the file using python, I get the string as

"{'FileID': 'a3333.txt','Timestamp': '2014-12-05T02:01:28.271Z','SuccessList':'a,b,c,d,e'}"

I want the double quotes to be removed from the beginning and end of the string. From python docs , I came to know that python adds double quotes by itself if there are single quotes in the string to avoid escaping.

Upvotes: 0

Views: 6100

Answers (4)

Geoff Wright
Geoff Wright

Reputation: 188

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.

Upvotes: 0

chucksmash
chucksmash

Reputation: 5997

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.

Upvotes: 4

vks
vks

Reputation: 67968

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'}"
print dict(re.findall(r"""'([^']*)'\s*:\s*'([^']*)'""",x))

Upvotes: 0

Wolf
Wolf

Reputation: 4452

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.

Upvotes: 0

Related Questions