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


Groups > comp.lang.python > #4506

Re: (beginner question) ConfigParser nuances

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <chris@rebertia.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'beginner': 0.04; 'python': 0.07; '"""': 0.09; '>>>>': 0.09; 'array.': 0.09; 'orange': 0.09; 'parsing': 0.09; 'subject:beginner': 0.09; 'pm,': 0.11; 'skip:f 30': 0.14; 'wrote:': 0.14; 'fruit': 0.16; 'moss': 0.16; 'name):': 0.16; 'subject:: (': 0.16; 'temp': 0.16; 'intermediate': 0.16; 'code.': 0.18; 'unknown': 0.19; 'cc:no real name:2**0': 0.20; 'cc:2**0': 0.20; 'cheers,': 0.20; 'seems': 0.21; 'variable': 0.21; 'not.': 0.22; 'header:In-Reply-To:1': 0.22; 'subject:question': 0.22; 'cc:addr:python-list': 0.22; 'convert': 0.22; 'mon,': 0.22; 'received:209.85.213': 0.23; "i'm": 0.26; 'instead': 0.26; 'chris': 0.27; 'message-id:@mail.gmail.com': 0.28; 'cc:addr:python.org': 0.31; 'nope,': 0.31; 'import': 0.32; '...': 0.32; 'array': 0.33; 'break': 0.33; 'thinking': 0.33; 'several': 0.33; 'options': 0.34; 'using': 0.34; 'there': 0.35; 'file': 0.35; 'question': 0.35; 'doing': 0.36; 'think': 0.36; 'some': 0.37; 'received:209.85': 0.37; 'received:google.com': 0.38; 'but': 0.38; 'common': 0.39; 'received:209': 0.39; 'how': 0.39; 'similar': 0.40; 'count': 0.40; 'header:Received:5': 0.40; 'might': 0.40; 'straight': 0.60; '2011': 0.62; 'here.': 0.68; 'numbered': 0.84; 'pear': 0.84; 'step.': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=domainkey-signature:mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=0f2uC/vBaXwUinXxHo+hXtkzp30SoGHhHzR9YUFj+mE=; b=LCrk8XfchJFuuIrY+1HI3TKArX1KgU8/KZwQeJ9Z57ZDX2U/YEW4Y1ODvDFgY7OCXP 97K1giK831GdMEni4HSvMKUFe7c4NV2/hfHopMkyY6SahuVWWX9BYs2sBeo36lXFRdAy kFeXjiCdSUsyxJbIZgGKJXpDfVByxVNgUhqWI=
DomainKey-Signature a=rsa-sha1; c=nofws; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; b=F6bxO93HvTgH280gErUQ2yQp0Y88QUDfVJyfTo1O49R4KUnrSt888px2zPF3gAIupR FyzTd4QWxTy3Yyt2VEAnFH+thhuPl9QcF/FJ+zxc4DGGU3I72ZlyUf1Vl7YAQFm6c+nG lBWywozLsZ1z75Z6SvrULqYzmIZXQWPvtplIA=
MIME-Version 1.0
Sender chris@rebertia.com
In-Reply-To <d53823a8-bc10-42b4-8fc0-91cc10e1234a@i39g2000prd.googlegroups.com>
References <d53823a8-bc10-42b4-8fc0-91cc10e1234a@i39g2000prd.googlegroups.com>
Date Mon, 2 May 2011 15:25:16 -0700
X-Google-Sender-Auth aKyf2-1thShiyKaktd8TyjooYCA
Subject Re: (beginner question) ConfigParser nuances
From Chris Rebert <clp2@rebertia.com>
To Unknown Moss <unknownmoss@gmail.com>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1082.1304375120.9059.python-list@python.org> (permalink)
Lines 50
NNTP-Posting-Host 82.94.164.166
X-Trace 1304375120 news.xs4all.nl 81474 [::ffff:82.94.164.166]:46045
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:4506

Show key headers only | View raw


On Mon, May 2, 2011 at 3:04 PM, Unknown Moss <unknownmoss@gmail.com> wrote:
> Hi - Beginner question here. I'm working with ConfigParser. I'd like
> to take a multiline variable and convert it directly to an array.
> Seems like a common  problem, but I don't see how I can do it without
> doing a little parsing in my own code. Here's what I'm doing ...
>
>>>> import ConfigParser
>>>> import io
>>>> sample = """
> ... [Example]
> ... fruit = apple
> ...     orange
> ...     pear
> ... """
>>>> config = ConfigParser.RawConfigParser()
>>>> config.readfp(io.BytesIO(sample))
>>>> config.get("Example", "fruit")
> 'apple\norange\npear'
>>>> temp = config.get("Example", "fruit")
>>>> temp.split()
> ['apple', 'orange', 'pear']
>
> I'm thinking there's a way to avoid this intermediate temp.split()
> step. Is there not a way to move a multiline value straight into an
> array using ConfigParser?

Nope, there is not. I think some might instead use several numbered
options to similar effect:

# config file
[Example]
fruit1: apple
fruit2: orange
fruit3: pear

# Python
from itertools import count
fruits = []
names = ("fruit" + str(i) for i in count(1))
for name in names:
    if not config.has_option("Example", name):
        break
    fruits.append(config.get("Example", name))


Cheers,
Chris
--
http://rebertia.com

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


Thread

(beginner question) ConfigParser nuances Unknown Moss <unknownmoss@gmail.com> - 2011-05-02 15:04 -0700
  Re: (beginner question) ConfigParser nuances Chris Rebert <clp2@rebertia.com> - 2011-05-02 15:25 -0700
    Re: (beginner question) ConfigParser nuances Unknown Moss <unknownmoss@gmail.com> - 2011-05-02 19:56 -0700

csiph-web