Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #98705

Re: can ConfigParser deal with repeating section header?

From Tim Chase <python.list@tim.thechases.com>
Newsgroups comp.lang.python
Subject Re: can ConfigParser deal with repeating section header?
Date 2015-11-12 09:59 -0600
Message-ID <mailman.276.1447346408.16136.python-list@python.org> (permalink)
References <95dc81c4-31eb-4f45-8b9c-20a03b2f0caa@googlegroups.com>

Show all headers | View raw


On 2015-11-12 07:47, John Zhao wrote:
> I have a configuration file with repeating sections, for example, 
> 
> [INSTANCE]
> Name=a
> 
> [INSTANCE]
> Name=b
> 
> I hope I can use ConfigParser to read the file and store the
> configuration settings in arrays.  
> 
> Is that possible?

Not with the standard library's ConfigParser:

  >>> from cStringIO import StringIO
  >>> from ConfigParser import ConfigParser
  >>> ini = StringIO("""[INSTANCE]
  ... Name=a
  ...
  ... [INSTANCE]
  ... Name=b
  ... """)
  >>> cp = ConfigParser()
  >>> cp.readfp(ini)
  >>> cp.sections() # note: only one section
  ['INSTANCE']
  >>> cp.get("INSTANCE", "Name") # note: only the last value
  'b'

It also fails to catch multiple values with the same key:

  [INSTANCE]
  Name=a
  Name=b

doesn't give you

  cp.get("INSTANCE", "Name") == ["a", "b"]


-tkc



Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

can ConfigParser deal with repeating section header? John Zhao <johnzzhao@gmail.com> - 2015-11-12 07:47 -0800
  Re: can ConfigParser deal with repeating section header? Tim Chase <python.list@tim.thechases.com> - 2015-11-12 09:59 -0600
  Re: can ConfigParser deal with repeating section header? amorawski@magna-power.com - 2015-11-12 11:38 -0800

csiph-web