Simple way to convert a string to a dictionary

后端 未结 10 2373
我在风中等你
我在风中等你 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:31

    This works for me:

    # get all the items
    matches = re.findall(r'\w+=".+?"', s) + re.findall(r'\w+=[\d.]+',s)
    
    # partition each match at '='
    matches = [m.group().split('=', 1) for m in matches]
    
    # use results to make a dict
    d = dict(matches)
    

提交回复
热议问题