[英]Extending configobj on python to handle custom comment marker
I have a config file, key and value pairs without sections.我有一个配置文件,没有部分的键和值对。 I'm trying to use configobj to parse it, but comments on my config files start with // rather than #
我正在尝试使用configobj来解析它,但对我的配置文件的注释以 // 而不是 # 开头
In the configobj documentation I found an example but it doesn't work to me:在 configobj 文档中,我找到了一个示例,但它对我不起作用:
from configobj import ConfigObj
class ConfigObjCustom(ConfigObj):
COMMENT_MARKERS = ['//']
config = ConfigObjCustom('hello.conf')
Is there a way to extend configobj to support other comment markers?有没有办法扩展 configobj 以支持其他注释标记?
I also could not get the suggested example with inheriting a new object with changed COMMENT_MARKERS
to work.我也无法通过继承新的 object 并更改
COMMENT_MARKERS
来获得建议的示例。
But the steamroller approach to solve the problem would be to read the file, replace the comments and then feed it to configobj.但是解决问题的压路机方法是读取文件,替换注释,然后将其提供给 configobj。 I did a quick test and it worked well enough:
我做了一个快速测试,效果很好:
from configobj import ConfigObj
import re
from io import StringIO
with open('hello.conf') as f:
file_contents = re.sub('//', '#', f.read())
print(config['SOMEKEY'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.