3

Imagine the following in python (using 2.7):

myString = '{ key: "value" }'

"value" could be a simple string, a list, a boolean or another dictionary, each of which really is in double quotes

If I try to convert this into a python dictionary or json, I fail because the key, in the original string is not quoted.

For example:

  • ast.literal_eval(myString) results in: IndentationError: unexpected indent or ValueError: malformed string (depending on the complexity of myString)
  • eval(myString) results in: NameError: name 'AbsolutePath' is not defined
  • json.loads(myString) results in: ValueError: Expecting property name enclosed in double quotes ...

Effectively, I am getting this string from a var declaration in javascript that is being scraped from the page. myString could be storing a dictionary with 50 to 100 entries.

I don't believe this is the same as asked elsewhere since the key is unquoted and the value is.

NAs
  • 89
  • 6
  • import ast import json myString = '{ key: "value" }' print (ast.literal_eval(myString)) print (eval(myString)) print (json.loads(myString)) – NAs Jun 23 '17 at 17:38
  • Possible duplicate of [How to convert raw javascript object to python dictionary?](https://stackoverflow.com/questions/24027589/how-to-convert-raw-javascript-object-to-python-dictionary) – Karl Reid Jun 23 '17 at 17:39
  • By scraping do you mean you are just pulling literal Javascript code into the string? I'm not sure how well that will work, if you can modify the javascript, use `JSON.stringify` and something like [demjson](http://deron.meranda.us/python/demjson/docs) to decode. – Karl Reid Jun 23 '17 at 17:42
  • Hmmm... I looked at the 'possible duplicate', but think that this is another variation of the same problem, but needing a different solution. And yes, by "scraping" I mean am literally pulling Javascript code into the string. I can't easily modify the javascript, but I suppose if I have to, I will. – NAs Jun 23 '17 at 17:57
  • I do think you will have to modify the code and export JSON instead of reading it. As it stands you're basically trying to directly translate a line of Javascript code into Python code, and that is just going to be a problem, they're not designed to interoperable. Better to use a defined intermediate format(i.e JSON) and parse that in your Python. But maybe someone knows something I don't and will answer. – Karl Reid Jun 23 '17 at 18:00

1 Answers1

0

If you're sure that only first key is without quotes, then what if you just simply add quotes in the strings manually? like this:

modified_strings_list=[]    
for string in string_list:
    string = string.replace('{','{"', 1)
    string = string.replace(':','":', 1)
    modified_strings_list.append(string)

And then apply eval()

any whitespaces can then easily be strip() -ed by looping dict.keys()

Dmitriy Fialkovskiy
  • 3,065
  • 8
  • 32
  • 47