I need to convert a line of SQL into a dictionary. Given the following partial line:
'field_1="value_1", field_2=1234'
I want to create the following dictionary:
{"field_1": "value_1", "field_2": 1234}
Using:
text = text.replace(",", "=")
text = text.split("=")
text = [i.strip() for i in text]
col_names = text[::2]
values = text[1::2]
dict_ = dict(zip(col_names, values))
I get as close as:
{'field_1': '"value_1"', 'field_2': '1234'}
I'm pretty sure I'll be able to sort out the extra quotation marks however I'm struggling with this line because of the comma inside the quotation marks wrecks my split():
'field_1="value_1a, value_1b", field_2=1234'
I have a feeling I might be able to use regex here to solve both my quotation mark and extra comma issues but I can't work out the exact syntax. The values can sometimes be strings and sometimes integers/floats. The field names can vary considerably and there aren't always spaces after commas. There also isn't a comma at the end of the string.
Thanks in advance!