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?