简体   繁体   中英

Convert string of list to list of values

I have a string of a list that I am trying to convert to list, however ast.literal_eval returns an error:

y = "[
        {
           'ake_ii': 88888, 
           'azefmlkh_amrlgba_dd': datetime.datetime(2022, 1, 31, 16, 52), 
           'sklmfs_qujdf_': datetime.datetime(2022, 1, 31, 23, 4)
        }
     ]"
ast.literal_eval(y)

error returned

ValueError: malformed node or string: <ast.Call object at 0x7fbaec22da60>

I want a list returned without it being a string.

Here's an answer adapted from my other related answer :

import ast
import datetime

data = """
[
        {
           'ake_ii': 88888, 
           'azefmlkh_amrlgba_dd': datetime.datetime(2022, 1, 31, 16, 52), 
           'sklmfs_qujdf_': datetime.datetime(2022, 1, 31, 23, 4)
        }
]
""".strip()


def literal_eval_with_datetime(source):
    # Adapted from `ast.literal_eval`
    def _convert(node):
        if isinstance(node, list):
            return [_convert(arg) for arg in node]
        if isinstance(node, ast.Constant):
            return node.value
        if isinstance(node, ast.Tuple):
            return tuple(map(_convert, node.elts))
        if isinstance(node, ast.List):
            return list(map(_convert, node.elts))
        if isinstance(node, ast.Set):
            return set(map(_convert, node.elts))
        if isinstance(node, ast.Call) and isinstance(node.func, ast.Name) and node.func.id == 'set' and node.args == node.keywords == []:
            return set()
        if isinstance(node, ast.Dict):
            return dict(zip(map(_convert, node.keys), map(_convert, node.values)))
        if isinstance(node, ast.Expression):
            return _convert(node.body)
        if isinstance(node, ast.Call) and ast.get_source_segment(source, node.func) == 'datetime.datetime':
            return datetime.datetime(*_convert(node.args))
        return {
            f'${node.__class__.__name__}': ast.get_source_segment(source, node),
        }

    return _convert(ast.parse(source, mode='eval'))


print(literal_eval_with_datetime(data))

The output is (a real list, of course):

[
    {
        "ake_ii": 88888,
        "azefmlkh_amrlgba_dd": datetime.datetime(2022, 1, 31, 16, 52),
        "sklmfs_qujdf_": datetime.datetime(2022, 1, 31, 23, 4),
    }
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM