Python中循环导入字典

Python中循环导入字典,python,python-import,Python,Python Import,我的问题与此有关。我有一个由字典组成的配置文件,如下所示: config_1 = { 'folder': 'raw1', 'start_date': '2019-07-01' } config_2 = { 'folder': 'raw2', 'start_date': '2019-08-01' } config_3 = { 'folder': 'raw3', 'start_date': '2019-09-01' } 然后,我有一个单独的pytho

我的问题与此有关。我有一个由字典组成的配置文件,如下所示:

config_1 = {
    'folder': 'raw1',
    'start_date': '2019-07-01'
}
config_2 = {
    'folder': 'raw2',
    'start_date': '2019-08-01'
}
config_3 = {
    'folder': 'raw3',
    'start_date': '2019-09-01'
}
然后,我有一个单独的python文件,用于导入每个配置并执行一些操作:

from config_file import config_1 as cfg1
Do some stuff using 'folder' and 'start_date'

from config_file import config_2 as cfg2
Do some stuff using 'folder' and 'start_date'

from config_file import config_2 as cfg3
Do some stuff using 'folder' and 'start_date'

我想把它放在一个循环中,而不是在python文件中列出3次。我该怎么做

您可以使用inspect模块从配置中获取所有可能的导入,如下所示

import config
import inspect

configs = [member[1] for member in inspect.getmembers(config) if 'config_' in member[0]]

configs
然后你可以迭代所有的配置,这是你想要的行为吗

您可以阅读更多关于inspect的信息


.

您可以使用inspect模块从配置中获取所有可能的导入,如下所示

import config
import inspect

configs = [member[1] for member in inspect.getmembers(config) if 'config_' in member[0]]

configs
然后你可以迭代所有的配置,这是你想要的行为吗

您可以阅读更多关于inspect的信息


.

如果我没弄错你的问题,就用吧。简而言之,您在python中编写的是:

from package import module as alias_mod
在importlib中,它变成:

alias_mod = importlib.import_module('module', 'package')
或者,相当于:

alias_mod = importlib.import_module('module.package')
例如:

from numpy import random as rm
在importlib中:

rm = importlib.import_module('random', 'numpy')
另一件有趣的事情是中提出的代码,它不仅允许您导入模块和包,还允许您直接导入函数等:

def import_from(module, name):
    module = __import__(module, fromlist=[name])
    return getattr(module, name)
对于您的特定情况,此代码应适用于:

import importlib

n_conf = 3
for in range(1, n_conf)
    conf = importlib.import_module('config_file.config_'+str(i))
    # todo somethings with conf 
但是,如果我能给你一些建议,我认为对你来说最好的办法是构建一个json配置文件并读取该文件,而不是导入模块。它舒服多了。例如,在您的案例中,您可以创建一个config.json文件,如下所示:

{
    "config_1": {
        "folder": "raw1",
        'start_date': '2019-07-01'
    },
    "config_2": {
        'folder': 'raw2',
        'start_date': '2019-08-01'
    },
    "config_3": {
        'folder': 'raw3',
        'start_date': '2019-09-01'
    }
}
按如下方式读取json文件:

import json
with open('config.json') as json_data_file:
    conf = json.load(json_data_file)
现在,内存中有一个简单的python字典,其中包含您感兴趣的配置设置:

conf['config_1']
# output: {'folder': 'raw1', 'start_date': '2019-07-01'}

如果我没弄错你的问题,就用。简而言之,您在python中编写的是:

from package import module as alias_mod
在importlib中,它变成:

alias_mod = importlib.import_module('module', 'package')
或者,相当于:

alias_mod = importlib.import_module('module.package')
例如:

from numpy import random as rm
在importlib中:

rm = importlib.import_module('random', 'numpy')
另一件有趣的事情是中提出的代码,它不仅允许您导入模块和包,还允许您直接导入函数等:

def import_from(module, name):
    module = __import__(module, fromlist=[name])
    return getattr(module, name)
对于您的特定情况,此代码应适用于:

import importlib

n_conf = 3
for in range(1, n_conf)
    conf = importlib.import_module('config_file.config_'+str(i))
    # todo somethings with conf 
但是,如果我能给你一些建议,我认为对你来说最好的办法是构建一个json配置文件并读取该文件,而不是导入模块。它舒服多了。例如,在您的案例中,您可以创建一个config.json文件,如下所示:

{
    "config_1": {
        "folder": "raw1",
        'start_date': '2019-07-01'
    },
    "config_2": {
        'folder': 'raw2',
        'start_date': '2019-08-01'
    },
    "config_3": {
        'folder': 'raw3',
        'start_date': '2019-09-01'
    }
}
按如下方式读取json文件:

import json
with open('config.json') as json_data_file:
    conf = json.load(json_data_file)
现在,内存中有一个简单的python字典,其中包含您感兴趣的配置设置:

conf['config_1']
# output: {'folder': 'raw1', 'start_date': '2019-07-01'}

根据@MikeMajara的评论,以下解决方案对我有效:

package = 'config_file'
configs = ['config_1', 'config_2', 'config_3']
for i in configs:
    cfg = getattr(__import__(package, fromlist=[configs]), i)
    Do some stuff using 'folder' and 'start_date'

根据@MikeMajara的评论,以下解决方案对我有效:

package = 'config_file'
configs = ['config_1', 'config_2', 'config_3']
for i in configs:
    cfg = getattr(__import__(package, fromlist=[configs]), i)
    Do some stuff using 'folder' and 'start_date'

你在所有三个配置文件上都做了同样的事情吗?是的,它们只是变量的值不同。因此,正如我第一次评论的那样,eval似乎不起作用。但另一个问题有另一个解决方案。从配置文件导入配置文件1、配置文件2、配置文件3?您对所有三个配置文件都做了相同的操作吗?是的,它们只是变量的值不同。因此,正如我第一次评论的那样,eval似乎不起作用。但另一个问题有另一个解决方案。从配置文件导入配置1、配置2、配置3?