4

I've inherited a python script that is pulling some variables from a default.conf file which I believe is a Machine configuration file.

One of the parts of the script is pulling a configuration key from the .conf file and expecting there to be a list of possible options however right now there is just one option and I'm unsure of how to make it so there are multiple options.

[syndication]
name = Test Name
title = Test Title
categories = Category 1

So in the above example the config key is syndication and the variable I'm trying to add multiple options to is category.

Thanks!

bigmike7801
  • 3,908
  • 9
  • 49
  • 77
  • Can you show is the segment of code that is causing problems? That will (hopefully) help us to figure out what should be in the conf file. – mgilson Jul 19 '12 at 21:38
  • If you need to read it with ConfigParser, parsing lists is not build in. My favourite to read in lists is this one: http://stackoverflow.com/a/9735884/1301710. Maybe you can switch to another config parser or json in general to store and parse your configuration files http://wiki.python.org/moin/ConfigParserShootout – bmu Jul 28 '12 at 06:51

4 Answers4

5

If there are too few values that ​​fit on one line, I would choose to separate them by commas as exemplified by other fellows, otherwise per RFC822 standard you can split values by lines started by tabs:

settings.conf:

[syndication]
name = Test Name
title = Test Title
categories =
    Category 1
    Category 2
    Category 3

settings.py:

#!/usr/bin/python
import ConfigParser

config = ConfigParser.ConfigParser()

# Reading
config.readfp(open('settings.conf'))
categories = config.get('syndication', 'categories').strip().split('\n')

# Appending
categories.append('Category 4')

# Changing
config.set('syndication', 'categories', '\n' + '\n'.join(categories))

# Storing
config.write(open('settings.conf', 'w'))

Your new settings.conf:

[syndication]
name = Test Name
title = Test Title
categories = 
    Category 1
    Category 2
    Category 3
    Category 4

Note: You can put a value in the first line after the : or =, but being a list of values, I think starting from the second line is more "readable" when you've to manually edit the file.

Community
  • 1
  • 1
Paulo Freitas
  • 13,194
  • 14
  • 74
  • 96
0

I don't think you have a list type in configuration files. However you can do something like comma delimited values:

[syndication]
name = Test Name
title = Test Title
categories = Category 1, Category 2

Then in your code split the values by ,

values = [value.strip() for value in cfg.get('syndication', 'categories').split(',')]
Miki Tebeka
  • 13,428
  • 4
  • 37
  • 49
0

You haven't mentioned how you're reading the .conf file, so I'll assume you're using ConfigParser.

I tried setting categories to a tuple and using ConfigParser.write, and ended up with a string representation of the tuple in the resulting file. This implies to me that ConfigParser doesn't support multiple options.

You can always break up an option manually:

categories = [option.strip() for option in config.get('syndication', 'categories').split(',')]
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
0

It is perhaps working the same way as python's logging configuration file format? In this case categories would be a comma separated list (In case of logging configuration files, every item is referring to a specific section in the config file)

gecco
  • 17,969
  • 11
  • 51
  • 68