简体   繁体   中英

ast.literal_eval for variables in python?

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?

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.

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