pro*_*eek 20 python ini configuration-files
通常,我按如下方式编码,以获取变量中的特定项目,如下所示
try:
config = ConfigParser.ConfigParser()
config.read(self.iniPathName)
except ConfigParser.MissingSectionHeaderError, e:
raise WrongIniFormatError(`e`)
try:
self.makeDB = config.get("DB","makeDB")
except ConfigParser.NoOptionError:
self.makeDB = 0
Run Code Online (Sandbox Code Playgroud)
有没有办法读取python字典中的所有内容?
例如
[A] x=1 y=2 z=3 [B] x=1 y=2 z=3
被写入
val["A"]["x"] = 1 ... val["B"]["z"] = 3
Ale*_*lli 31
我建议子类化ConfigParser.ConfigParser(或SafeConfigParser,&c)安全地访问"受保护"属性(以单下划线开头的名称 - "private"将是以两个下划线开头的名称,即使在子类中也不能访问...):
import ConfigParser
class MyParser(ConfigParser.ConfigParser):
def as_dict(self):
d = dict(self._sections)
for k in d:
d[k] = dict(self._defaults, **d[k])
d[k].pop('__name__', None)
return d
Run Code Online (Sandbox Code Playgroud)
这模拟了配置解析器的通常逻辑,并保证在所有版本的Python中都有效,其中有一个ConfigParser.py模块(最多2.7个,这是该2.*系列的最后一个- 知道将来不会有Python 2.任何版本都是如何保证兼容性;-).
如果你需要支持未来的Python 3.*版本(最多3.1版本,可能是即将推出的3.2版本应该没问题,只需将模块重命名为全小写configparser而非当然),未来几年可能需要一些关注/调整,但是我不指望任何重大的东西.
pro*_*eek 27
我设法得到答案,但我希望应该有一个更好的答案.
dictionary = {}
for section in config.sections():
dictionary[section] = {}
for option in config.options(section):
dictionary[section][option] = config.get(section, option)
Run Code Online (Sandbox Code Playgroud)
And*_*rew 10
ConfigParser的实例数据在内部存储为嵌套的dict.您可以复制它,而不是重新创建它.
>>> import ConfigParser
>>> p = ConfigParser.ConfigParser()
>>> p.read("sample_config.ini")
['sample_config.ini']
>>> p.__dict__
{'_defaults': {}, '_sections': {'A': {'y': '2', '__name__': 'A', 'z': '3', 'x': '1'}, 'B': {'y': '2', '__name__': 'B', 'z': '3', 'x': '1'}}, '_dict': <type 'dict'>}
>>> d = p.__dict__['_sections'].copy()
>>> d
{'A': {'y': '2', '__name__': 'A', 'z': '3', 'x': '1'}, 'B': {'y': '2', '__name__': 'B', 'z': '3', 'x': '1'}}
Run Code Online (Sandbox Code Playgroud)
编辑:
Alex Martelli的解决方案更清洁,更强大,更漂亮.虽然这是公认的答案,但我建议改用他的方法.有关详细信息,请参阅他对此解决方案的评论
Evg*_*pin 10
我知道这个问题是5年前提出的,但是今天我已经把这个词汇理解得很好了:
parser = ConfigParser()
parser.read(filename)
confdict = {section: dict(parser.items(section)) for section in parser.sections()}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
35721 次 |
| 最近记录: |