0

I have a file containing lots of python dict. I want to load some of those dict as requested by the user. How cant I do that ?

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'}

The following code does not work

for i in ['A','C']:
    from my_dicts_file import i

I don't want to load all the dicts if possible with

from my_dicts_file import *

2 Answers2

0

You can pickle the variables and then load when you need them. To do that you can use Python's pickle module

rabbit
  • 21
  • 4
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