Python 配置解析器读取注释和值
我有配置文件,
[local]
variable1 : val1 ;#comment1
variable2 : val2 ;#comment2
这样的代码只读取键的值:
class Config(object):
def __init__(self):
self.config = ConfigParser.ConfigParser()
self.config.read('config.py')
def get_path(self):
return self.config.get('local', 'variable1')
if __name__ == '__main__':
c = Config()
print c.get_path()
但我也想阅读当前的注释和值,这方面的任何建议都会非常有帮助。
I have config file,
[local]
variable1 : val1 ;#comment1
variable2 : val2 ;#comment2
code like this reads only value of the key:
class Config(object):
def __init__(self):
self.config = ConfigParser.ConfigParser()
self.config.read('config.py')
def get_path(self):
return self.config.get('local', 'variable1')
if __name__ == '__main__':
c = Config()
print c.get_path()
but i also want to read the comment present along with the value, any suggestions in this regards will be very helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
唉,这在一般情况下并不容易做到。注释应该被解析器忽略。
在您的具体情况下,这很容易,因为
#
仅在一行开始时充当注释字符。因此,variable1 的值将是"val1 #comment1"
。我想你使用这样的东西,只是不那么脆弱:如果需要“评论”的值,可能它不是一个正确的评论?考虑为“评论”添加显式键,如下所示:
Alas, this is not easily done in general case. Comments are supposed to be ignored by the parser.
In your specific case, it is easy, because
#
only serves as a comment character if it begins a line. So variable1's value will be"val1 #comment1"
. I suppose you use something like this, only less brittle:If the value of a 'comment' is needed, probably it is not a proper comment? Consider adding explicit keys for the 'comments', like this:
您唯一的解决方案是编写另一个
ConfigParser
覆盖方法_read()
。在您的 ConfigParser 中,您应该删除所有有关评论删除的检查。这是一个危险的解决方案,但应该可行。在 ValuesWithCommentsConfigParser 中,我修复了一些导入并删除了相应的代码部分。
使用我的之前的答案中的相同
config.ini
,我可以证明之前的代码是正确的。Your only solutions is to write another
ConfigParser
overriding the method_read()
. In yourConfigParser
you should delete all checks about comment removal. This is a dangerous solution, but should work.In the
ValuesWithCommentsConfigParser
I fixed some imports and deleted the appropriate sections of code.Using the same
config.ini
from my previous answer, I can prove the previous code is correct.根据 ConfigParser 模块 文档,
如果要读取带有值的“注释”,可以省略
;
字符之前的空格或使用#
。但在这种情况下,字符串comment1
和comment2
成为值的一部分,不再被视为注释。更好的方法是使用不同的属性名称,例如
variable1_comment
,或者在配置中定义专门用于注释的另一个部分:第一个解决方案要求您使用另一个密钥生成一个新密钥(即计算
variable1_comment
fromvariable1
),另一个允许您使用针对配置文件中不同部分的相同密钥。从 Python 2.7.2 开始,如果使用
#
字符,始终可以沿行读取注释。正如文档所说,这是为了向后兼容。以下代码应该顺利运行:对于以下
config.ini
文件:如果您采用此解决方案,请记住手动解析
get()
结果中的值和注释。Accordiing to the ConfigParser module documentation,
If you want to read the "comment" with the value, you can omit the whitespace before the
;
character or use the#
. But in this case the stringscomment1
andcomment2
become part of the value and are not considered comments any more.A better approach would be to use a different property name, such as
variable1_comment
, or to define another section in the configuration dedicated to comments:The first solution requires you to generate a new key using another one (i.e. compute
variable1_comment
fromvariable1
), the other one allows you to use the same key targeting different sections in the configuration file.As of Python 2.7.2, is always possibile to read a comment along the line if you use the
#
character. As the docs say, it's for backward compatibility. The following code should run smoothly:for the following
config.ini
file:If you adopt this solution, remember to manually parse the result of
get()
for values and comments.根据手册:
以“#”或“;”开头的行被忽略并可用于提供评论。
所以variable1的值是“val1 #comment1”。注释是值的一部分,
您可以检查您的配置是否在注释前输入了Enter键
according to the manuals:
Lines beginning with '#' or ';' are ignored and may be used to provide comments.
so the value of variable1 is "val1 #comment1".The comment is part of the value
you can check your config whether you put a Enter before your comment
万一后来有人过来。我的情况是我需要读取 Pascal 应用程序生成的 .ini 文件。该 configparser 不关心 # 或 ;启动按键。
例如,.ini 文件看起来像
Python 的 configparser 会跳过该键值对。需要修改configparser以不将#视为注释
我确信我可以将comment_prefixes设置为Pascal用于注释的任何内容,但没有看到任何内容,所以我将其设置为空字符串
In case anyone comes along afterwards. My situation was I needed to read in a .ini file generated by a Pascal Application. That configparser didn't care about # or ; starting the keys.
For example the .ini file would look like this
Python's configparser would skip that key value pair. Needed to modify the configparser to not look at # as comments
I'm sure I could set the comment_prefixes to whatever Pascal uses for comments, but didn't see any, so I set it to an empty string