ast.literal_eval for variables in python?

倖福魔咒の 提交于 2019-12-30 07:16:06

问题


Suppose I have a file example.py: import example

VVV = 2

DictionaryNameB = {
    'a'                   : VVV,
    'bb'                  : 'SomethingB',
    'c'                   : False,
    'ccc'                 : None,
    'dddd'                : 'true',
    'eeeee'               : 0.123456,
    'f'                   : 2,
    'h'                   : [1,2,3]
}

I wrote a function that uses ast.literal_eval():

def getDicFromFile(self, dic_name):
    with open( 'example.py' ) as f:
        file_data = f.read()
    match = re.findall('%s[^{]+\{[^\}]+\}' % dic_name, file_data, re.MULTILINE)[0]
    # print(match)
    dicObject = ast.literal_eval(match[len(dic_name)+3:])
    return dicObject

I got the error raise ValueError('malformed string') ; ValueError: malformed string

I understand that ast.literal_eval() can't decode the variable VVV, is there another way to do it?


回答1:


You could use asteval, a library that builds on the ast parse tree to execute limited statements. It'll handle your example out of the box:

from asteval import Interpreter

aeval = Interpreter()
aeval(file_data)
dicObject = aeval.symtable['DictionaryNameB']

Or, you could just import the file:

from importlib import import_module
module = import_module('example')
dicObject = module.DictionaryNameB

asteval allows for quite a wide number of Python constructs. You could compare the Python Abstract Grammar with what handlers the aeval.node_handlers mapping registers, and simply delete any you don't need. You could remove function definitions and calling, looping, binary operations (binop) and exception handling, for example.



来源:https://stackoverflow.com/questions/25867475/ast-literal-eval-for-variables-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!