Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #60475 > unrolled thread
| Started by | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| First post | 2013-11-25 18:29 -0600 |
| Last post | 2013-11-25 18:29 -0600 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: general ConfigParser question Tim Chase <python.list@tim.thechases.com> - 2013-11-25 18:29 -0600
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2013-11-25 18:29 -0600 |
| Subject | Re: general ConfigParser question |
| Message-ID | <mailman.3211.1385425670.18130.python-list@python.org> |
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 top | Article view | comp.lang.python
csiph-web