Convert stringified list of dictionaries back to a list of dictionaries

前端 未结 3 357
时光取名叫无心
时光取名叫无心 2021-01-19 04:17

I know that to convert a dictionary to/from a string, I use json.loads and json.dumps. However, those methods fail when given a string representing

3条回答
  •  星月不相逢
    2021-01-19 04:46

    A well-formed python data type can in most case be interpreted using ast.literal_eval

    import ast
    ast.literal_eval("[{'topic': 'obama', 'interval': 'daily', 'type': 'test'}, {'topic': 'biden', 'interval': 'immediate', 'type': 'test'}]")
    Out[38]: 
    [{'interval': 'daily', 'topic': 'obama', 'type': 'test'},
     {'interval': 'immediate', 'topic': 'biden', 'type': 'test'}]
    

提交回复
热议问题