使用Python的ConfigParser读取一个没有节名的文件

我正在使用ConfigParser来读取脚本的运行时configuration。

我想有一个灵活的不提供一个部分的名字(有足够简单的脚本,他们不需要一个“部分”)。 ConfigParser将抛出NoSectionErrorexception,并不会接受该文件。

我怎样才能使ConfigParser简单地检索configuration文件的(key, value)元组没有部分名称? 例如:

 key1=val1 key2:val2 

我宁愿不写入configuration文件。

Alex Martelli 提供了使用ConfigParserparsing.properties文件(显然是无ConfigParserconfiguration文件)的解决scheme。

他的解决scheme是一个类似文件的包装器,它将自动插入一个虚拟段标题来满足ConfigParser的要求。

通过jterrace的这个回答启发 ,我想出了这个解决scheme:

  1. 将整个文件读入一个string
  2. 带有默认部分名称的前缀
  3. 使用StringIO来模拟一个类似文件的对象
 ini_str = '[root]\n' + open(ini_path, 'r').read() ini_fp = StringIO.StringIO(ini_str) config = ConfigParser.RawConfigParser() config.readfp(ini_fp) 

你可以用一个额外的代码行来做到这一点。 (如果您计算import语句,则为两行)

在python 3中,使用itertools.chain()来模拟read_file()的section标题。

 from configparser import ConfigParser from itertools import chain parser = ConfigParser() with open("foo.conf") as lines: lines = chain(("[top]",), lines) # This line does the trick. parser.read_file(lines) 

在python 2中,在你从configuration文件中读取的数据前加一个标题行,将结果包装在一个StringIO对象中,并传递给readfp()

 from ConfigParser import ConfigParser from StringIO import StringIO parser = ConfigParser() with open("foo.conf") as stream: stream = StringIO("[top]\n" + stream.read()) # This line does the trick. parser.readfp(stream) 

无论采用哪种方法,您的configuration设置都将在parser.items('top')

你也可以在python 3中使用StringIO ,通过从io包中的新家中导入它,但是注意readfp()在python 3中被弃用,因此应该避免。

你可以使用ConfigObj库来做到这一点: http ://www.voidspace.org.uk/python/configobj.html

更新: 在这里find最新的代码。

如果你在Debian / Ubuntu下,你可以使用你的软件包pipe理器来安装这个模块:

 apt-get install python-configobj 

使用示例:

 from configobj import ConfigObj config = ConfigObj('myConfigFile.ini') config.get('key1') # You will get val1 config.get('key2') # You will get val2 

自己遇到这个问题,我写了一个完整的包装到ConfigParser(在Python 2中的版本),可以读取和写入文件没有章节透明地基于亚历克斯·马尔泰利的方法链接在接受的答案。 它应该是任何使用ConfigParser的替代品。 发布它,以防万一需要的人发现此页面。

 import ConfigParser import StringIO class SectionlessConfigParser(ConfigParser.RawConfigParser): """ Extends ConfigParser to allow files without sections. This is done by wrapping read files and prepending them with a placeholder section, which defaults to '__config__' """ def __init__(self, *args, **kwargs): default_section = kwargs.pop('default_section', None) ConfigParser.RawConfigParser.__init__(self, *args, **kwargs) self._default_section = None self.set_default_section(default_section or '__config__') def get_default_section(self): return self._default_section def set_default_section(self, section): self.add_section(section) # move all values from the previous default section to the new one try: default_section_items = self.items(self._default_section) self.remove_section(self._default_section) except ConfigParser.NoSectionError: pass else: for (key, value) in default_section_items: self.set(section, key, value) self._default_section = section def read(self, filenames): if isinstance(filenames, basestring): filenames = [filenames] read_ok = [] for filename in filenames: try: with open(filename) as fp: self.readfp(fp) except IOError: continue else: read_ok.append(filename) return read_ok def readfp(self, fp, *args, **kwargs): stream = StringIO() try: stream.name = fp.name except AttributeError: pass stream.write('[' + self._default_section + ']\n') stream.write(fp.read()) stream.seek(0, 0) return ConfigParser.RawConfigParser.readfp(self, stream, *args, **kwargs) def write(self, fp): # Write the items from the default section manually and then remove them # from the data. They'll be re-added later. try: default_section_items = self.items(self._default_section) self.remove_section(self._default_section) for (key, value) in default_section_items: fp.write("{0} = {1}\n".format(key, value)) fp.write("\n") except ConfigParser.NoSectionError: pass ConfigParser.RawConfigParser.write(self, fp) self.add_section(self._default_section) for (key, value) in default_section_items: self.set(self._default_section, key, value) 

最简单的方法是使用Python的CSVparsing器,在我看来。 这是一个读/写function,演示这种方法以及testing驱动程序。 这应该工作,只要值不允许是多行的。 🙂

 import csv import operator def read_properties(filename): """ Reads a given properties file with each line of the format key=value. Returns a dictionary containing the pairs. Keyword arguments: filename -- the name of the file to be read """ result={ } with open(filename, "rb") as csvfile: reader = csv.reader(csvfile, delimiter='=', escapechar='\\', quoting=csv.QUOTE_NONE) for row in reader: if len(row) != 2: raise csv.Error("Too many fields on row with contents: "+str(row)) result[row[0]] = row[1] return result def write_properties(filename,dictionary): """ Writes the provided dictionary in key-sorted order to a properties file with each line of the format key=value Keyword arguments: filename -- the name of the file to be written dictionary -- a dictionary containing the key/value pairs. """ with open(filename, "wb") as csvfile: writer = csv.writer(csvfile, delimiter='=', escapechar='\\', quoting=csv.QUOTE_NONE) for key, value in sorted(dictionary.items(), key=operator.itemgetter(0)): writer.writerow([ key, value]) def main(): data={ "Hello": "5+5=10", "World": "Snausage", "Awesome": "Possum" } filename="test.properties" write_properties(filename,data) newdata=read_properties(filename) print "Read in: " print newdata print contents="" with open(filename, 'rb') as propfile: contents=propfile.read() print "File contents:" print contents print ["Failure!", "Success!"][data == newdata] return if __name__ == '__main__': main() 

Blueicefield的答案提到了configobj,但是原始的lib只支持Python 2.它现在有一个Python 3+兼容的端口:

https://github.com/DiffSK/configobj

API没有改变,看到它的文档 。

configobj lib可以帮助在情况下KEY="value"

 from configobj import ConfigObj cfg = ConfigObj('/home/.aws/config') access_key_id = cfg['aws_access_key_id'] secret_access_key = cfg['aws_secret_access_key']