Simple way to convert a string to a dictionary

后端 未结 10 2405
我在风中等你
我在风中等你 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条回答
  •  猫巷女王i
    2020-12-11 18:16

    do it step by step

    d={}
    mystring='name="John Smith", age=34, height=173.2, location="US", avatar=":,=)"';
    s = mystring.split(", ")
    for item in s:
        i=item.split("=",1)
        d[i[0]]=i[-1]
    print d
    

提交回复
热议问题