Python: How would you save a simple settings / configuration file?

I don't care if it's JSON, pickle, YAML or something else.

All other implementations that I have seen are not compatible with forwards, so if I have a configuration file, add a new key to the code, then load this configuration file, it will just work.

Is there an easy way to do this?

+44
json python settings config ini
Sep 29 '13 at 12:44 on
source share
4 answers

Python configuration files

There are several ways to do this, depending on the desired file format.

ConfigParser [.ini format]

I would use standard configparser if there was no good reason to use a different format.

Write a file like this:

from ConfigParser import SafeConfigParser config = SafeConfigParser() config.read('config.ini') config.add_section('main') config.set('main', 'key1', 'value1') config.set('main', 'key2', 'value2') config.set('main', 'key3', 'value3') with open('config.ini', 'w') as f: config.write(f) 

The file format is very simple with sections highlighted in square brackets:

 [main] key1 = value1 key2 = value2 key3 = value3 

Values ​​can be extracted from the file as follows:

 from ConfigParser import SafeConfigParser config = SafeConfigParser() config.read('config.ini') print config.get('main', 'key1') # -> "value1" print config.get('main', 'key2') # -> "value2" print config.get('main', 'key3') # -> "value3" # getfloat() raises an exception if the value is not a float a_float = config.getfloat('main', 'a_float') # getint() and getboolean() also do this for their respective types an_int = config.getint('main', 'an_int') 

JSON [.json format]

JSON data can be very complex and have the advantage of being very portable.

Writing data to a file:

 import json config = {'key1': 'value1', 'key2': 'value2'} with open('config.json', 'w') as f: json.dump(config, f) 

Reading data from a file:

 import json with open('config.json', 'r') as f: config = json.load(f) #edit the data config['key3'] = 'value3' #write it back to the file with open('config.json', 'w') as f: json.dump(config, f) 

Yaml

A basic YAML example is provided in this answer . More information can be found on the pyYAML website .

+89
Sep 29 '13 at 13:42 on
source share
β€” -

If you want to use something like an INI file to store settings, consider using configparser , which loads pairs of key values ​​from a text file, and can easily write back to the file.

INI file has the format:

 [Section] key = value key with spaces = somevalue 
+8
Sep 29 '13 at 12:52 on
source share

Basic ConfigParser Example

The file can be downloaded and used as follows:

 #!/usr/bin/env python import ConfigParser import io # Load the configuration file with open("config.yml") as f: sample_config = f.read() config = ConfigParser.RawConfigParser(allow_no_value=True) config.readfp(io.BytesIO(sample_config)) # List all contents print("List all contents") for section in config.sections(): print("Section: %s" % section) for options in config.options(section): print("x %s:::%s:::%s" % (options, config.get(section, options), str(type(options)))) # Print some contents print("\nPrint some contents") print(config.get('other', 'use_anonymous')) # Just get the value print(config.getboolean('other', 'use_anonymous')) # You know the datatype? 

which outputs

 List all contents Section: mysql x host:::localhost:::<type 'str'> x user:::root:::<type 'str'> x passwd:::my secret password:::<type 'str'> x db:::write-math:::<type 'str'> Section: other x preprocessing_queue:::["preprocessing.scale_and_center", "preprocessing.dot_reduction", "preprocessing.connect_lines"]:::<type 'str'> x use_anonymous:::yes:::<type 'str'> Print some contents yes True 

As you can see, you can use a standard data format that is easy to read and write. Methods like getboolean and getint allow you to get a data type instead of a simple string.

Record Configuration

 import os configfile_name = "config.yaml" # Check if there is already a configurtion file if not os.path.isfile(configfile_name): # Create the configuration file as it doesn't exist yet cfgfile = open(configfile_name, 'w') # Add content to the file Config = ConfigParser.ConfigParser() Config.add_section('mysql') Config.set('mysql', 'host', 'localhost') Config.set('mysql', 'user', 'root') Config.set('mysql', 'passwd', 'my secret password') Config.set('mysql', 'db', 'write-math') Config.add_section('other') Config.set('other', 'preprocessing_queue', ['preprocessing.scale_and_center', 'preprocessing.dot_reduction', 'preprocessing.connect_lines']) Config.set('other', 'use_anonymous', True) Config.write(cfgfile) cfgfile.close() 

leads to

 [mysql] host = localhost user = root passwd = my secret password db = write-math [other] preprocessing_queue = ['preprocessing.scale_and_center', 'preprocessing.dot_reduction', 'preprocessing.connect_lines'] use_anonymous = True 

Basic XML Example

It doesn't seem to be used at all for the Python community configuration files. However, parsing / writing XML is simple, and with Python there are many possibilities for this. One of them is BeautifulSoup:

 from BeautifulSoup import BeautifulSoup with open("config.xml") as f: content = f.read() y = BeautifulSoup(content) print(y.mysql.host.contents[0]) for tag in y.other.preprocessing_queue: print(tag) 

where config.xml might look like this:

 <config> <mysql> <host>localhost</host> <user>root</user> <passwd>my secret password</passwd> <db>write-math</db> </mysql> <other> <preprocessing_queue> <li>preprocessing.scale_and_center</li> <li>preprocessing.dot_reduction</li> <li>preprocessing.connect_lines</li> </preprocessing_queue> <use_anonymous value="true" /> </other> </config> 
+8
Jan 11 '15 at 19:41
source share

Save and load dictionary. You will have arbitrary keys, values ​​and an arbitrary number of key pairs, values.

+1
Sep 29 '13 at 12:48 on
source share



All Articles