0

I can read a configuration file with a list like this:

# config file is called config.txt
[PARAMETER]
site_loc      = [45.77,-87.20]

# Read in site_loc list
import  ast
from ConfigParser import SafeConfigParser

# Parse config file
parser = SafeConfigParser()

parser.read('config.txt')

Is there a way to read a list of lists? e.g. site_loc = [[45.77,-87.20], [23.0,-45.0]]

7
  • I can't get your example to work... It's giving me an empty list when I do parser.read('config.txt'). Also, I think that function wants a file, not a string... Commented Oct 16, 2015 at 1:08
  • I had to do parser.read(open('config.txt','r')) to get the empty list. Otherwise it gave me ['config.txt'] Commented Oct 16, 2015 at 1:08
  • rofls, you need to create the config.txt file. The data in config.txt is site_loc... Commented Oct 16, 2015 at 1:09
  • 1
    I did create it... :) Commented Oct 16, 2015 at 1:09
  • >>> t=open('config.txt','r') >>> for i in t: ... print i ... [PARAMETER] site_loc = [45.77,-87.20] Commented Oct 16, 2015 at 1:10

1 Answer 1

1

For more complex config files like yours, it's probably better to use a third party configuration library. For example:

https://github.com/toml-lang/toml

https://github.com/avakar/pytoml

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.