ktflghm
ktflghm

Reputation: 169

In Python, How to remove various unwanted quotes in a string to create/convert to a dictionary

I have a string in the following form:

testline = "{""key1"": ""value1"", ""key2"": {""value2-subkey1"": ""value2-subvalue2""}}"

I would like to replace the double-double quotes with a single-double quote (") and strip the initial and final double quote to finish with a dictionary.

So far, I've got something like this, which is very much not doing what I want.

import ast
# testline = testline.strip(")
testline = testline.replace('""', '"')
testlinedict = ast.literal_eval(testline)

This so far yields ValueError: malformed string

I want the final result to be:

testlinedict = {"key1": "value1", "key2": {"value2-subkey1": "value2-subvalue2"}}

Upvotes: 0

Views: 6270

Answers (3)

Niklas B.
Niklas B.

Reputation: 95358

The problem is that the double quotes are actually interpreted by Python, but not in the way you expected:

>>> testline = "{""key1"": ""value1"", ""key2"": {""value2-subkey1"": ""value2-subvalue2""}}"
>>> testline
'{key1: value1, key2: {value2-subkey1: value2-subvalue2}}'

This is because in Python, like in C, several string literals following each other are interpreted as one large string, so "abc""def" == "abcdef".

If you define testdata correctly, your solution works:

>>> testline = '{""key1"": ""value1"", ""key2"": {""value2-subkey1"": ""value2-subvalue2""}}'
>>> literal_eval(testline.replace('""', '"'))
{'key2': {'value2-subkey1': 'value2-subvalue2'}, 'key1': 'value1'}

Or, in case the first and last quote are actually part of the string:

>>> testline = '"{""key1"": ""value1"", ""key2"": {""value2-subkey1"": ""value2-subvalue2""}}"'
>>> literal_eval(testline[1:-1].replace('""', '"'))
{'key2': {'value2-subkey1': 'value2-subvalue2'}, 'key1': 'value1'}

Upvotes: 6

Sid
Sid

Reputation: 7631

testline=testline.replace("\"\"","\"")[1:-1]

Upvotes: 1

Amber
Amber

Reputation: 527378

testline = testline.replace('""', '"')
testline = testline[1:-1]

Replace the doubled doublequotes first, then just take off the first and last characters to remove the leading and trailing doublequotes.

If you actually want to wind up with a dictionary object, rather than a string representation of one, you should then use something like ast.literal_eval() to load the string as Python code (or json.loads() to load it as JSON).

Upvotes: 0

Related Questions