Here is a more detailed approach to the problem using pyparsing. Pay attention to parsing actions that perform automatic type conversion from strings to int or float. Also, the QuotedString class implicitly removes quotation marks from the specified value. In conclusion, the Dict class takes each group "key = val" in a comma-separated list and assigns result names using key tokens and values.
from pyparsing import * key = Word(alphas) EQ = Suppress('=') real = Regex(r'[+-]?\d+\.\d+').setParseAction(lambda t:float(t[0])) integer = Regex(r'[+-]?\d+').setParseAction(lambda t:int(t[0])) qs = QuotedString('"') value = real | integer | qs dictstring = Dict(delimitedList(Group(key + EQ + value)))
Now parse the original text string by storing the results in dd. Pyparsing returns an object of type ParseResults, but this class has many features similar to dict (support for keys (), items (), in, etc.) or it can emit a true Python dict by calling asDict (). The dump call () shows all the tokens in the original parsed list, plus all the named items. The last two examples show how to access named elements in ParseResults as if they were attributes of a Python object.
text = 'name="John Smith", age=34, height=173.2, location="US", avatar=":,=)"' dd = dictstring.parseString(text) print dd.keys() print dd.items() print dd.dump() print dd.asDict() print dd.name print dd.avatar
Print
['age', 'location', 'name', 'avatar', 'height'] [('age', 34), ('location', 'US'), ('name', 'John Smith'), ('avatar', ':,=)'), ('height', 173.19999999999999)] [['name', 'John Smith'], ['age', 34], ['height', 173.19999999999999], ['location', 'US'], ['avatar', ':,=)']] - age: 34 - avatar: :,=) - height: 173.2 - location: US - name: John Smith {'age': 34, 'height': 173.19999999999999, 'location': 'US', 'avatar': ':,=)', 'name': 'John Smith'} John Smith :,=)