(beginner question) ConfigParser nuances

Discussion in 'Python' started by Unknown Moss, May 2, 2011.

  1. Unknown Moss

    Unknown Moss Guest

    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?

    Thanks for the help.
     
    Unknown Moss, May 2, 2011
    #1
    1. Advertising

  2. Unknown Moss

    Chris Rebert Guest

    On Mon, May 2, 2011 at 3:04 PM, Unknown Moss <> 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
     
    Chris Rebert, May 2, 2011
    #2
    1. Advertising

  3. Unknown Moss

    Unknown Moss Guest

    On May 2, 3:25 pm, Chris Rebert <> wrote:
    > On Mon, May 2, 2011 at 3:04 PM, Unknown Moss <> wrote:
    > > Hi -Beginnerquestionhere. 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


    Ok, thanks Chris. Maybe I'm getting too lazy in my old age. :)
     
    Unknown Moss, May 3, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    621
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  2. Roy H. Berger

    Subclassing ConfigParser question

    Roy H. Berger, Nov 25, 2003, in forum: Python
    Replies:
    2
    Views:
    509
    Gary Richardson
    Nov 26, 2003
  3. Replies:
    3
    Views:
    442
  4. Ruby Freak

    Please explain nuances of ||=

    Ruby Freak, May 1, 2008, in forum: Ruby
    Replies:
    26
    Views:
    299
    Simon Krahnke
    May 2, 2008
  5. Richard Leber

    Nuances of alias_method

    Richard Leber, Apr 12, 2010, in forum: Ruby
    Replies:
    1
    Views:
    120
    Robert Dober
    Apr 12, 2010
Loading...

Share This Page