(beginner question) ConfigParser nuances

U

Unknown Moss

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 ...
.... [Example]
.... fruit = apple
.... orange
.... pear
.... """['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.
 
C

Chris Rebert

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 ...
... [Example]
... fruit = apple
...     orange
...     pear
... """['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
 
U

Unknown Moss

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. :)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top