Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将值从字典放入变量_Python_Python 3.x - Fatal编程技术网

Python 如何将值从字典放入变量

Python 如何将值从字典放入变量,python,python-3.x,Python,Python 3.x,我尝试从我的ini文件的节中获取所有值(通过configparser)作为变量: hue310section = dict(parser.items('HUE_310')) for keys, value in hue310section.items(): pairs = keys + ' = ' + value print(pairs) 它给了我partnewfilepath=http://some_site:PORT/about,但我不知道如何将此输出作为python变量

我尝试从我的
ini
文件的节中获取所有值(通过configparser)作为变量:

hue310section = dict(parser.items('HUE_310'))

for keys, value in hue310section.items():
    pairs = keys + ' = ' + value
    print(pairs)
它给了我
partnewfilepath=http://some_site:PORT/about
,但我不知道如何将此输出作为python变量导入,我可以在代码中的某个地方使用
partnewfilepath
。当然,一个部分的值会比一个部分的值多,我想在变量中更改所有这些值。我试图找到解决方案,但我想我错过了一些东西,因为我对python的了解还不够。我想我需要重新构建我的
for
语句,但在这个特殊问题上,我不知道该怎么做

我的config.ini文件如下所示:

[HUE_310]
partNewFilePath = ${common:domain}/about
otherValues = something
nextvalue = another something
更新:

我想我需要详细说明我想要实现的目标。在代码的另一部分,我检查了我想要处理的站点的版本。如果站点有,比如说3.10版,我想从我的
ini
文件中获取
HUE_310
部分的所有值,并将它们用作python变量。我的其余代码使用这些变量,如果站点版本将更改,我可以从我的
ini
文件的其他部分获取值,并将这些值获取到python变量并使用它们。我假设一些变量会随着版本的变化而变化,这就是为什么我要准备代码来检查这一点。同时,它也给了我一些自由修改一些变量,如果网站将改变。
我希望现在更清楚。

您不需要新的变量或for循环,您已经有了
hue310section
dict

你可以用

hue310section['partNewFilePath']
这等于

"http://some_site:PORT/about"
注意,在
hue310section=dict(parser.items('HUE_310'))之后
其他值
下一个值
键也将被定义

from configobj import ConfigObj

parser_data = ConfigObj(config_path)
current = parser_data['HUE_310'].get('partNewFilePath', 'http://www.default.com')
config_path是文件的路径


是在找不到特定密钥的情况下的默认值。

有一个名为'example.com'cf的特殊域,但我需要更通用的解决方案。我假设我不知道配置文件部分中有什么。我只想将特定部分中的所有内容作为变量导入,以便在我的代码中的某个地方使用它们。所以我需要从HUE_310节中提取所有信息,当我发现我只需要该节中的信息时,将它们作为变量放入我的代码中。@heniekk:
dict
将准确地包含这些信息。您可以迭代它们的键,也可以只获取所需的键。定义变量没有额外的好处。我编辑了我的问题,并详细阐述了我想用我的代码实现什么。请现在检查。