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


Groups > comp.lang.python > #98702 > unrolled thread

can ConfigParser deal with repeating section header?

Started byJohn Zhao <johnzzhao@gmail.com>
First post2015-11-12 07:47 -0800
Last post2015-11-12 11:38 -0800
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  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

#98702 — can ConfigParser deal with repeating section header?

FromJohn Zhao <johnzzhao@gmail.com>
Date2015-11-12 07:47 -0800
Subjectcan ConfigParser deal with repeating section header?
Message-ID<95dc81c4-31eb-4f45-8b9c-20a03b2f0caa@googlegroups.com>
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?

Thanks a lot.

John

[toc] | [next] | [standalone]


#98705

FromTim Chase <python.list@tim.thechases.com>
Date2015-11-12 09:59 -0600
Message-ID<mailman.276.1447346408.16136.python-list@python.org>
In reply to#98702
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



[toc] | [prev] | [next] | [standalone]


#98711

Fromamorawski@magna-power.com
Date2015-11-12 11:38 -0800
Message-ID<d6f0cc21-dfcc-4572-85aa-777daf59112b@googlegroups.com>
In reply to#98702
On Thursday, November 12, 2015 at 10:48:11 AM UTC-5, 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?
> 
> Thanks a lot.
> 
> John

Here is one of possible solutions:
http://stackoverflow.com/questions/9876059/parsing-configure-file-with-same-section-name-in-python

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web