使用Python将ini文件中的所有内容读取到字典中
通常,我的代码如下,用于获取变量中的特定项目,如下所示
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
有没有办法读取 python 字典中的所有内容?
例如
[A] x=1 y=2 z=3 [B] x=1 y=2 z=3
写入
val["A"]["x"] = 1 ... val["B"]["z"] = 3
Normally, I code as follows for getting a particular item in a variable as follows
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
Is there any way to read all the contents in a python dictionary?
For example
[A] x=1 y=2 z=3 [B] x=1 y=2 z=3
is written into
val["A"]["x"] = 1 ... val["B"]["z"] = 3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
另一件需要注意的事情是,ConfigParser 会将键值转换为小写,因此,如果您将配置条目转换为字典,请交叉检查您的要求。因此我遇到了一个问题。对我来说,我有驼峰式键,因此,当我开始使用字典而不是文件时,必须更改一些代码。
ConfigParser.get()
方法在内部将密钥转换为小写。One more thing to take care is,
ConfigParser
converts the key values to lowercase hence in case you are converting the config entries to a dictionary cross check your requirements. I faced a problem because of this. For me I was having camel-case keys hence, had to change some amount of code when I started using the dictionary instead of files.ConfigParser.get()
method internally converts the key to lower-case.来自 https://wiki.python.org/moin/ConfigParserExamples
from https://wiki.python.org/moin/ConfigParserExamples
我建议子类化
ConfigParser.ConfigParser
(或SafeConfigParser
, &c)以安全地访问“受保护”属性(以单下划线开头的名称 - “private”将是名称以两个下划线开头,即使在子类中也不能访问...):这模拟了配置解析器的通常逻辑,并且保证在有 ConfigParser 的所有 Python 版本中工作.py 模块(最高 2.7,这是
2.*
系列的最后一个 - 知道未来不会有 Python 2.any 版本,这就是兼容性的 >保证;-)。如果您需要支持未来的 Python
3.*
版本(最高为 3.1,可能还有即将推出的 3.2),只需将模块重命名为全小写的configparser
即可当然)几年后它可能需要一些关注/调整,但我不期望发生任何重大的事情。I suggest subclassing
ConfigParser.ConfigParser
(orSafeConfigParser
, &c) to safely access the "protected" attributes (names starting with single underscore -- "private" would be names starting with two underscores, not to be accessed even in subclasses...):This emulates the usual logic of config parsers, and is guaranteed to work in all versions of Python where there's a
ConfigParser.py
module (up to 2.7, which is the last of the2.*
series -- knowing that there will be no future Python 2.any versions is how compatibility can be guaranteed;-).If you need to support future Python
3.*
versions (up to 3.1 and probably the soon forthcoming 3.2 it should be fine, just renaming the module to all-lowercaseconfigparser
instead of course) it may need some attention/tweaks a few years down the road, but I wouldn't expect anything major.我设法得到了答案,但我希望应该有更好的答案。
I managed to get an answer, but I expect there should be a better one.
我知道这个问题是 5 年前提出的,但今天我把这个 dict 理解变成了这样:
I know that this question was asked 5 years ago, but today I've made this dict comprehension thingy:
ConfigParser 的实例数据在内部存储为嵌套字典。您可以复制它,而不是重新创建它。
编辑:
Alex Martelli 的 解决方案更干净、更健壮、更漂亮。虽然这是公认的答案,但我建议改用他的方法。有关更多信息,请参阅他对此解决方案的评论。
The instance data for ConfigParser is stored internally as a nested dict. Instead of recreating it, you could just copy it.
Edit:
Alex Martelli's solution is cleaner, more robust, and prettier. While this was the accepted answer, I'd suggest using his approach instead. See his comment to this solution for more info.
如何在py中解析ini文件?
其中 test.ini 文件包含:
How to parse ini file in py?
Where test.ini file contain: