python - Proper way to deal with string which looks like json object but it is wrapped with single quote -


by definition json string wrapped double quote.

in fact:

json.loads('{"v":1}') #works

json.loads("{'v':1}") #doesn't work

but how deal second statements? i'm looking solution different eval or replace. thanks.

not sure if got requirements right, looking this?

def fix_json(string_):     if string_[0] == string_[-1] == "'":         return '"' + string_[1:-1] +'"'     return string_ 

example usage:

>>> fix_json("'{'key':'val\"'...cd'}'") "{'key':'val"'...cd'}" 

edit: seems humour tried have in making example above not self-explanatory. so, here's example:

>>> fix_json("'this string has - i'm sure -  single quotes delimiters.'") "this string has - i'm sure -  single quotes delimiters." 

this examples show how "replacement" happens @ extremities of string, not within it.

you achieve same regular expression, of course, if checking starting , finishing char of string, find using regular string indexes more readable....


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -