-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfigfile_example.py
More file actions
39 lines (27 loc) · 897 Bytes
/
configfile_example.py
File metadata and controls
39 lines (27 loc) · 897 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python
"""
Example of how to save data as an "ini" file with ConfigParser
"""
import ConfigParser
outfilename = "addresses.ini"
# get the data from the py file
# ini format really only holds flat data well.
from add_book_data_flat import AddressBook
# save it in an ini file
data = ConfigParser.ConfigParser()
for i, person in enumerate(AddressBook):
sec_name = 'person%i'%i
data.add_section(sec_name)
for key,value in person.items():
data.set(sec_name, key, value)
data.write( open("add_book.ini",'w') )
## see if we can re-load it
data = data = ConfigParser.ConfigParser()
data.read("add_book.ini")
#extract the data and put into a list of dicts:
AddressBook2 = []
for sec_name in data.sections():
AddressBook2.append( dict( data.items(sec_name) ) )
print AddressBook2
if AddressBook2 == AddressBook:
print "they are the same"