1

Im having issues with the following code trying to parse a string to dictionary because of escaping characters , I will post an example :

string = """ {"key" : "value 'others' = \"one \" "} """
json.loads(string)

json cannot load because it gets the following string with too many double quotes :

 {"key" : "value 'others' = "one " "}

In fact I dont need to load with json , I could simply do an eval :

ast.literal_eval(string)

So far so good, problem is Im not able to delete the escaped double quotes strings with replace (example : s.replace('\"','') ), so , there is some low level string manipulation to achieve what I want to ? I would like to get a valid json syntax :

{"key" : "value 'others' = one "} 

Or be able to get python not deleting backslash :

{"key" : "value 'others' = \"one\" "} 

As Francois stated this can be achieved with a raw string :

rawstring = r"my string \""

My problem is related to the string placed already into a non raw string variable, so I need some way to get that non raw string to be recasted to raw string in order to parse to a dictionary data structure using json or ast.literal_eval

Flechoide
  • 75
  • 3
  • 10
  • Don't you want to additionaly escape the quotes such that JSON sees it as: `{"key" : "value 'others' = \"one \" "}`. Now you will throw away information. – Willem Van Onsem Jan 23 '17 at 10:33
  • Hi Willem, in fact it is escaped that way in the string that comes from the read : {"key" : "value 'others' = \"one \" "} . Problem here is that string interprets the double quotes with backslash because is defined as a normal string – Flechoide Jan 24 '17 at 12:08
  • A raw string is not a different type from a regular string. It's just a different literal notation in python code for convenience. Once the string literal has been parsed, it's no different from any other string. It's similar to how python lets you declare string literals using either double or single quotes. – Håken Lid Jan 24 '17 at 16:47
  • How did you load the string? You can use `repr(s)` on the string `s` to see what the string looks like, according to python. If any string literals in json data contain inner `"` characters, those characters must be escaped properly if you want to parse the json. – Håken Lid Jan 24 '17 at 16:54
  • This explains raw strings: http://stackoverflow.com/questions/2081640/what-exactly-do-u-and-r-string-flags-do-in-python-and-what-are-raw-string-l – Håken Lid Jan 24 '17 at 17:03

1 Answers1

2

The quote escaping didn't work here:

>>> """ {"key" : "value 'others' = \"one \" "} """
' {"key" : "value \'others\' = "one " "} '

as you see, the backslashes have been ignored. You could take them into account with the raw prefix:

import json
string = r""" {"key" : "value 'others' = \"one \" "} """
print(json.loads(string))

result:

{'key': 'value \'others\' = "one " '}
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • Yes, thats true, but how do I cast the string into raw string ? In case I have the whole string into a var named string : string = """ {"key" : "value 'others' = \"one \" "} """ Something like : rawString = string.toRaw() – Flechoide Jan 23 '17 at 11:20
  • raw string is just a prefix that you use with _literals_. If the string comes from a file and it has quotes, no need to raw-it. – Jean-François Fabre Jan 23 '17 at 22:09
  • The problem is that Im not parsing from file but from variable because I get that info from browser mechanize simulating a web navigation, so, I have the stringo into a python variable, I have something like that : string = browser.response().read() – Flechoide Jan 24 '17 at 08:59
  • so your question is different. You should edit it with a printed value of the string you're receiving. – Jean-François Fabre Jan 24 '17 at 09:02
  • The string Im receiving is quite long, but I have a variable filled with a string comingo from a read() method from the browser data structure , the sample string I placed is smaller but representative enough for the purpose of the question : string = """ {"key" : "value 'others' = \"one \" "} """ and I want to parse that to a dict, in order to achieve that I need that string to be interpreted as raw because of the mix of quotes and double quotes. I have already edited the first question of the post, by the way thanks a lot for the help – Flechoide Jan 24 '17 at 12:00
  • 1
    you don't understand: typing it literally like you did removes the backslashes. You'd have to put the string in a file and read from it for it to be representative of a non-literal data. – Jean-François Fabre Jan 24 '17 at 12:48
  • Sorry I was so happy that I didnt read the part of result. Thanks :) – Flechoide Jan 24 '17 at 18:34