我有一个包含大量python dict的文件。我想根据用户的要求加载其中一些dict。我怎么能这样做?
my_dicts_file.py:
A = { 'value_a': 0,
'value_b':'aa'}
B = { 'value_a': 10,
'value_b':'bb',
'value_c':'Nd'}
....
....
Z = { 'value_4': 50,
'value_t':'0d'}
以下代码不起作用
for i in ['A','C']:
from my_dicts_file import i
如果可能的话,我不想加载所有的词汇
from my_dicts_file import *
答案 0 :(得分:0)
您可以挑选变量,然后在需要时加载。要做到这一点,你可以使用Python的pickle模块
答案 1 :(得分:0)
I'm not sure if it can do direct import like that, but I create some dummy example here.
import imp
my_dict = type('lambdaobject', (object, ), {})()
for i in ['A', 'C']:
src = imp.load_source(i, 'my_dicts_file.py')
setattr(mydict, i, getattr(src, i))
say if you're wanted to print variable A, you just
print my_dict.A