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


Groups > comp.lang.python > #33440

Re: editing conf file

Date 2012-11-16 07:43 -0600
From Tim Chase <python.list@tim.thechases.com>
Subject Re: editing conf file
References <k85a7i$sjn$1@poprovec.arnes.si> <k85aoe$uot$1@poprovec.arnes.si> <996a6d6c-fdb2-40a0-bd5c-ada2ba35af78@i2g2000pbi.googlegroups.com> <k85cnl$84o$1@poprovec.arnes.si> <20121116130411.GA4246@taris.box>
Newsgroups comp.lang.python
Message-ID <mailman.3751.1353073345.27098.python-list@python.org> (permalink)

Show all headers | View raw


On 11/16/12 07:04, Thomas Bach wrote:
> On Fri, Nov 16, 2012 at 01:48:49PM +0100, chip9munk wrote:
>> configparser has four functions: get, getboolean, getfloat and getint.
>>
>> how do I get list from cfg file?!
> 
> AFAIK you have to parse the list yourself. Something like
> 
> my_list = [ s.strip() for s in cp.get('section', 'option').split(',') ]
> 
> if you use a comma as a separator.

For slightly more complex option text, you can use the CSV module to
do the heavy lifting, so if you have something like

  [section]
  option="one, one, one",two,3

then you can have Python give you

  my_list = next(csv.reader([cp.get("section", "option")]))

or alternatively use the shlex module and separate them like shell
options:

  [section]
  option="one, one, one" two 3

then do

  my_list = list(shlex.shlex(cp.get("section", "option")))

Or yet one more way using Python list syntax for literals:

  [section]
  option=["one, one, one", "two", 3]

and get them with

  my_list = ast.literal_eval(cp.get("section", "option))

Lots of fun (and batteries-included) ways depending on how you want
to represent the list in the config file and what sorts of data it
can contain.

-tkc




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


Thread

editing conf file chip9munk <"chip9munk[SSSpAm"@gmail.com> - 2012-11-16 13:06 +0100
  Re: editing conf file chip9munk <"chip9munk[SSSpAm"@gmail.com> - 2012-11-16 13:15 +0100
    Re: editing conf file rusi <rustompmody@gmail.com> - 2012-11-16 04:35 -0800
      Re: editing conf file chip9munk <"chip9munk[SSSpAm"@gmail.com> - 2012-11-16 13:48 +0100
        Re: editing conf file Thomas Bach <thbach@students.uni-mainz.de> - 2012-11-16 14:04 +0100
          Re: editing conf file chip9munk <"chip9munk[SSSpAm"@gmail.com> - 2012-11-16 14:28 +0100
        Re: editing conf file Tim Chase <python.list@tim.thechases.com> - 2012-11-16 07:43 -0600
  Re: editing conf file Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-11-16 14:02 +0100
    Re: editing conf file chip9munk <"chip9munk[SSSpAm"@gmail.com> - 2012-11-16 14:27 +0100
      Re: editing conf file Roy Smith <roy@panix.com> - 2012-11-16 09:08 -0500
        Re: editing conf file rusi <rustompmody@gmail.com> - 2012-11-16 08:41 -0800

csiph-web