使用python使用字典配置文件
因此,我尝试在配置文件中使用字典来存储API调用的报告名。比如说:使用python使用字典配置文件,python,python-2.7,dictionary,config,configobj,Python,Python 2.7,Dictionary,Config,Configobj,因此,我尝试在配置文件中使用字典来存储API调用的报告名。比如说: report = {'/report1': '/https://apicall...', '/report2': '/https://apicall...'} from configobj import ConfigObj config = ConfigObj('settings.ini', unrepr=True) for x in config['report']: # do something... p
report = {'/report1': '/https://apicall...', '/report2': '/https://apicall...'}
from configobj import ConfigObj
config = ConfigObj('settings.ini', unrepr=True)
for x in config['report']:
# do something...
print x
我需要存储多个报告:apicall到一个配置值。我正在使用ConfigObj。我在那里读过,上面说我应该能做到。我的代码如下所示:
report = {'/report1': '/https://apicall...', '/report2': '/https://apicall...'}
from configobj import ConfigObj
config = ConfigObj('settings.ini', unrepr=True)
for x in config['report']:
# do something...
print x
但是,当它点击config=时,会抛出一个raise错误。我在这里有点迷路了。我甚至复制并粘贴了他们的例子和同样的东西,“提出错误”。我正在使用python27并已安装configobj库。您的配置文件
settings.ini
应采用以下格式:
[report]
/report1 = /https://apicall...
/report2 = /https://apicall...
如果要使用
unrepr=True
,则需要使用此配置文件作为输入:
report = {'/report1': '/https://apicall...', '/report2': '/https://apicall...'}
此配置文件用作输入
flag = true
report = {'/report1': '/https://apicall...', '/report2': '/https://apicall...'}
生成此异常,该异常与您得到的异常类似:
O:\_bats>configobj-test.py
Traceback (most recent call last):
File "O:\_bats\configobj-test.py", line 43, in <module>
config = ConfigObj('configobj-test.ini', unrepr=True)
File "c:\Python27\lib\site-packages\configobj.py", line 1242, in __init__
self._load(infile, configspec)
File "c:\Python27\lib\site-packages\configobj.py", line 1332, in _load
raise error
configobj.UnreprError: Unknown name or type in value at line 1.
O:\\u bats>configobj-test.py
回溯(最近一次呼叫最后一次):
文件“O:\\u bats\configobj test.py”,第43行,在
config=ConfigObj('ConfigObj-test.ini',unrepr=True)
文件“c:\Python27\lib\site packages\configobj.py”,第1242行,在\uuu init中__
自加载(填充、配置规范)
文件“c:\Python27\lib\site packages\configobj.py”,第1332行,在加载中
提出错误
configobj.Unprerror:第1行的名称或输入值未知。
启用unrepr
模式后,需要使用有效的Python关键字。在我的示例中,我使用了true
而不是true
。我猜您的settings.ini
中还有一些其他设置导致了异常
unrepr选项允许您使用配置文件存储和检索基本Python数据类型。它必须使用与普通ConfigObj文件稍有不同的语法。毫不奇怪,它使用Python语法。这意味着列表是不同的(它们被方括号包围),字符串必须被引用
unrepr可以使用的类型有:
字符串、列表、元组无、对、错
字典、整数、浮点数
长复数
如果您不必使用<代码> INI 文件,您可以考虑使用另一种更适合处理<代码> DICT < /C> >对象的文件格式。查看您给出的示例文件,您可以使用
JSON
文件,Python有一个模块来处理它
例如:
JSON文件“settings.JSON”:
Python代码:
import json
with open("settings.json") as jsonfile:
# `json.loads` parses a string in json format
reports_dict = json.load(jsonfile)
for report in reports_dict['report']:
# Will print the dictionary keys
# '/report1', '/report2'
print report
我在尝试读取ini文件时遇到类似问题:
[Section]
Value: {"Min": -0.2 , "Max": 0.2}
最终使用了配置解析器和json的组合:
import ConfigParser
import json
IniRead = ConfigParser.ConfigParser()
IniRead.read('{0}\{1}'.format(config_path, 'config.ini'))
value = json.loads(IniRead.get('Section', 'Value'))
显然,可以使用其他文本文件解析器,因为json加载只需要json格式的字符串。我遇到的一个问题是dictionary/json字符串中的键需要用双引号括起来。抛出了哪个错误?你能在这里粘贴完整的堆栈跟踪吗?