使用ConfigParser读取没有节名的文件 [英] Using ConfigParser to read a file without section name
问题描述
我正在使用ConfigParser
来读取脚本的运行时配置.
I am using ConfigParser
to read the runtime configuration of a script.
我想不提供部分名称的灵活性(有些脚本很简单;它们不需要部分"). ConfigParser
将引发NoSectionError
异常,并且将不接受该文件.
I would like to have the flexibility of not providing a section name (there are scripts which are simple enough; they don't need a 'section'). ConfigParser
will throw a NoSectionError
exception, and will not accept the file.
如何使ConfigParser仅仅检索没有节名的配置文件的(key, value)
元组?
How can I make ConfigParser simply retrieve the (key, value)
tuples of a config file without section names?
例如:
key1=val1
key2:val2
我宁愿不写配置文件.
推荐答案
Alex Martelli provided a solution for using ConfigParser
to parse .properties
files (which are apparently section-less config files).
他的解决方案是一个类似于文件的包装程序,将自动插入一个虚拟节标题来满足ConfigParser
的要求要求.
His solution is a file-like wrapper that will automagically insert a dummy section heading to satisfy ConfigParser
's requirements.
这篇关于使用ConfigParser读取没有节名的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!