Convert stringified list of dictionaries back to a list of dictionaries

前端 未结 3 356
时光取名叫无心
时光取名叫无心 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'}]
    
    0 讨论(0)
  • 2021-01-19 04:56

    You may use ast.literal_eval:

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

    As per the ast.literal_eval document, it:

    Safely evaluate an expression node or a Unicode or Latin-1 encoded string containing a Python literal or container display. The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None.

    0 讨论(0)
  • 2021-01-19 05:00

    Converting that to a string with str() gives us...

    There's your problem right there. You shouldn't be using str to serialize data. There's no guarantee that the resulting string will be something that can be recovered back into the original object.

    Instead of using str(), use json.dumps. I know you said you tried it and it didn't work, but it's quite robust when used on built-in objects, so I suspect you may have just accidentally made a typo. The example dict you give serializes perfectly:

    >>> sample_entry = [
    ...     {"type": "test", "topic": "obama", "interval": "daily"},
    ...     {"type": "test", "topic": "biden", "interval": "immediate"},
    ... ]
    >>>
    >>> s = json.dumps(sample_entry)
    >>> print(s)
    [{"interval": "daily", "topic": "obama", "type": "test"}, {"interval": "immediate", "topic": "biden", "type": "test"}]
    >>>
    >>> d = json.loads(s)
    >>> d
    [{'interval': 'daily', 'topic': 'obama', 'type': 'test'}, {'interval': 'immediate', 'topic': 'biden', 'type': 'test'}]
    
    0 讨论(0)
提交回复
热议问题