Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Tim Chase Newsgroups: comp.lang.python Subject: Re: can ConfigParser deal with repeating section header? Date: Thu, 12 Nov 2015 09:59:54 -0600 Lines: 47 Message-ID: References: <95dc81c4-31eb-4f45-8b9c-20a03b2f0caa@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de Isx2a1HRB76erhUuRNEoKgSoHQ6ie3yTyeZ7YnwSjetA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'cc:addr:python-list': 0.09; '""")': 0.09; 'stringio': 0.09; "'b'": 0.16; '-tkc': 0.16; 'arrays.': 0.16; 'cstringio': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'key:': 0.16; 'possible?': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; '>>>': 0.20; 'settings': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'cc:no real name:2**0': 0.22; 'import': 0.24; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'values': 0.28; 'file': 0.34; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'charset:us-ascii': 0.37; 'subject:with': 0.40; 'john': 0.61; 'hope': 0.61; 'received:50': 0.66; 'note:': 0.66; 'configparser': 0.84; 'ini': 0.84 X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-MC-Relay: Neutral X-MailChannels-SenderId: wwwh|x-authuser|tim@thechases.com X-MailChannels-Auth-Id: wwwh X-MC-Loop-Signature: 1447344082132:599569410 X-MC-Ingress-Time: 1447344082132 In-Reply-To: <95dc81c4-31eb-4f45-8b9c-20a03b2f0caa@googlegroups.com> X-Mailer: Claws Mail 3.11.1 (GTK+ 2.24.25; x86_64-pc-linux-gnu) X-AuthUser: tim@thechases.com X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:98705 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