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


Groups > comp.lang.python > #60475

Re: general ConfigParser question

Date 2013-11-25 18:29 -0600
From Tim Chase <python.list@tim.thechases.com>
Subject Re: general ConfigParser question
References <CAOF-KfgVxVHL1KFBQbWyfn8JE9Ht7mNPK8=+krydMFVs6PcGyw@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.3211.1385425670.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 2013-11-25 18:32, Rita wrote:
> I was wondering if the default ConfigParser can handle multi line
> strings (especially in the relate section)
> 
> [Relate]
> data="parent process A child process B
> Parent process B child process C

Yes, though I seem to recall that subsequent lines have to be
indented:

>>> from ConfigParser import ConfigParser as cp
>>> from StringIO import StringIO
>>> sio = StringIO("""[global]
... one ="first
...     second
...     third"
... """)
>>> c = cp()
>>> c.readfp(sio)
>>> c.get("global", "one")  # Note2: quotes
'"first\nsecond\nthird"'
>>> sio = StringIO("""[global]
... one =first
...     second
...     third
...""")
>>> c = cp()
>>> c.readfp(sio)
>>> c.get("global", "one")  # Note2: no quotes
'first\nsecond\nthird'

Note1: this also strips off leading whitespace on the subsequent
lines.

Note2: if you quote the values, they appear in the resulting value.

So based on this, I occasionally do like CherryPy does and require
quotes, then wrap my cp.get(section, name) results in a
ast.literal_eval() call to get the corresponding Python result.
Alternatively, you can use the cp.getint(), cp.getbool() and
cp.getfloat() calls.

-tkc


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


Thread

Re: general ConfigParser question Tim Chase <python.list@tim.thechases.com> - 2013-11-25 18:29 -0600

csiph-web