使用 ConfigParser 读取非标准配置文件
Using ConfigParser to read non-standard config files
我有一个格式为
的配置文件
# foo.conf
[section1]
foo=bar
buzz=123
[section2]
line1
line2
line3
我想使用 Python ConfigParser
库进行解析。请注意,section2
不包含 key/value 对,而是一些原始文本。我希望有可能将 section2
的所有(原始)内容读取到变量中。
ConfigParser
是否允许我阅读此文件,或者它的 类 之一是否可以以一种简单的方式进行子类化以这样做?
使用标准
import ConfigParser
config = ConfigParser.ConfigParser()
config.read('foo.conf')
产量ConfigParser.ParsingError: File contains parsing errors: foo.conf
据我所知,ConfigParser 不能这样做:
The ConfigParser class implements a basic configuration file parser
language which provides a structure similar to what you would find on
Microsoft Windows INI files.
看来你的conf文件不是一个基本的配置文件,所以也许有两种方法可以解析这个conf文件。
- 读取conf文件并修改。
- 生成有效的配置文件。
您可以尝试使用 io 适配器将输入文件转换为适合 ConfigParser 的格式。一种方法是转换既不是空行也不是注释行的普通行,也不是 linei=original_line
中的 key=value
行的部分行,其中 i 在每行增加并从 1 开始部分。
可能的代码可能是:
class ConfParsAdapter(io.RawIOBase):
@staticmethod
def _confParsAdapter(fd):
num=1
rxsec = re.compile('\[.*\]( *#.*)?$')
rxkv = re.compile('.+?=.*')
rxvoid = re.compile('(#.*)?$')
for line in fd:
if rxsec.match(line.strip()):
num=1
elif rxkv.match(line) or rxvoid.match(line.strip()):
pass
else:
line = 'line{}={}'.format(num, line)
num += 1
yield(line)
def __init__(self, fd):
self.fd = self._confParsAdapter(fd)
def readline(self, hint = -1):
try:
return next(self.fd)
except StopIteration:
return ""
这样,您可以使用当前文件而无需更改其中的任何内容:
>>> parser = ConfigParser.RawConfigParser()
>>> parser.readfp(ConfParsAdapter(open('foo.conf'))
>>> parser.sections()
['section1', 'section2']
>>> parser.items('section2')
[('line1', 'line1'), ('line2', 'line2'), ('line3', 'line3')]
>>>
我有一个格式为
的配置文件# foo.conf
[section1]
foo=bar
buzz=123
[section2]
line1
line2
line3
我想使用 Python ConfigParser
库进行解析。请注意,section2
不包含 key/value 对,而是一些原始文本。我希望有可能将 section2
的所有(原始)内容读取到变量中。
ConfigParser
是否允许我阅读此文件,或者它的 类 之一是否可以以一种简单的方式进行子类化以这样做?
使用标准
import ConfigParser
config = ConfigParser.ConfigParser()
config.read('foo.conf')
产量ConfigParser.ParsingError: File contains parsing errors: foo.conf
据我所知,ConfigParser 不能这样做:
The ConfigParser class implements a basic configuration file parser language which provides a structure similar to what you would find on Microsoft Windows INI files.
看来你的conf文件不是一个基本的配置文件,所以也许有两种方法可以解析这个conf文件。
- 读取conf文件并修改。
- 生成有效的配置文件。
您可以尝试使用 io 适配器将输入文件转换为适合 ConfigParser 的格式。一种方法是转换既不是空行也不是注释行的普通行,也不是 linei=original_line
中的 key=value
行的部分行,其中 i 在每行增加并从 1 开始部分。
可能的代码可能是:
class ConfParsAdapter(io.RawIOBase):
@staticmethod
def _confParsAdapter(fd):
num=1
rxsec = re.compile('\[.*\]( *#.*)?$')
rxkv = re.compile('.+?=.*')
rxvoid = re.compile('(#.*)?$')
for line in fd:
if rxsec.match(line.strip()):
num=1
elif rxkv.match(line) or rxvoid.match(line.strip()):
pass
else:
line = 'line{}={}'.format(num, line)
num += 1
yield(line)
def __init__(self, fd):
self.fd = self._confParsAdapter(fd)
def readline(self, hint = -1):
try:
return next(self.fd)
except StopIteration:
return ""
这样,您可以使用当前文件而无需更改其中的任何内容:
>>> parser = ConfigParser.RawConfigParser()
>>> parser.readfp(ConfParsAdapter(open('foo.conf'))
>>> parser.sections()
['section1', 'section2']
>>> parser.items('section2')
[('line1', 'line1'), ('line2', 'line2'), ('line3', 'line3')]
>>>