Simple way to convert a string to a dictionary

后端 未结 10 2403
我在风中等你
我在风中等你 2020-12-11 17:36

What is the simplest way to convert a string of keyword=values to a dictionary, for example the following string:

name=\"John Smith\", age=34, height=173.2,          


        
10条回答
  •  醉酒成梦
    2020-12-11 18:41

    I would suggest a lazy way of doing this.

    test_string = 'name="John Smith", age=34, height=173.2, location="US", avatar=":,=)"'
    eval("dict({})".format(test_string))
    

    {'age': 34, 'location': 'US', 'avatar': ':,=)', 'name': 'John Smith', 'height': 173.2}

    Hope this helps someone !

提交回复
热议问题