问题
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