1👍
✅
The easiest thing would probably be to serialize to JSON or YAML. However, if you want a more human-readable format that is more strictly a set of key-value pairs, you might use the ConfigParser module. It reads and writes INI-style configuration files, like this:
[section]
key = value
Here is an example of writing out a configuration file based on a form’s cleaned_data
:
In [39]: from ConfigParser import SafeConfigParser
In [40]: cleaned_data = {'foo': 'bar', 'bob': 'ann'}
In [41]: config = SafeConfigParser()
In [42]: config.add_section('my_form')
In [43]: for field, value in cleaned_data.iteritems():
....: config.set('my_form', field, value)
....:
In [44]: with open('example.cfg', 'w') as config_file:
....: config.write(config_file)
....:
In [45]: cat example.cfg
[my_form]
bob = ann
foo = bar
Source:stackexchange.com