I got this code from somewhere online, and saved it in a variable called "doc".
doc = """{"Grade": " \"B+\" "}"""
I want doc to print
{"Grade": " B+ "}
So that I can use ast.literal_eval() to convert "doc" into a dictionary.
But when I try:
print(doc)
It prints:
{"Grade": " "B+" "}
Which is not what I want because then ast.literal_eval() won't work.
Or:
print(doc.replace("\"", ''))
which gives me:
{Grade: B+ }
It completely removes the double quote, which is not what I want either because ast.literal_eval() gives an error.
So how can I change "doc" so that
doc = """{"Grade": " \"B+\" "}"""
can print the following code after some work?
{"Grade": " B+ "}
Thanks in advance!
We can just convert the repeated double quotes to a single set of double quotes with re.sub()
:
In [95]: doc = """{"Grade": " \"B+\" "}"""
In [96]: doc
Out[96]: '{"Grade": " "B+" "}'
In [98]: re.sub(r'["]\s*["]', '"', doc)
Out[98]: '{"Grade": "B+"}'
In [99]: import ast
In [101]: doc = re.sub(r'["]\s*["]', '"', doc)
In [102]: ast.literal_eval(doc)
Out[102]: {'Grade': 'B+'}
Ok I assume that you have a string in the form {"key": "value"}
in which the value could contain unquoted double quotes. In your example, we have Grade
for key and "B+"
for value.
In that case, you can either remove the inner quotes or correctly quote them, but you should split the string to identify the value part
start, value, last = re.match(r'(\s*{\s*".*?"\s*:\s*")(.*)("\s*})', doc).groups()
You can then easily process quotes in the value part:
fixed = value.replace('"', "") # remove quotes
or
fixed = value.replace('"', r'\"') # correctly quotes the double quotes
You can then successfully write:
d = ast.litteral_eval(start + fixed + last)
and get either {'Grade': ' B+ '}
or {'Grade': ' "B+" '}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With