使用dict4ini
http://code.google.com/p/dict4ini/
import dict4ini
x = dict4ini.DictIni('test.ini')
x.common.name = 'limodou'
x.common.bool = 1
x.common.list = [3, 1.2, 'Hello', 'have spaces']
x.save()
This example will save option to test.ini. As you can see, you needn't create section "common" at first, just think it's there, it's ok. The result of test.ini is:
[common]
list = 3,1.2,Hello,"have spaces",
bool = 1
name = limodou
And you can see, once the value has special chars, just like ' ', ',', '/"', etc, the string will be quoted by double quoter. But "Hello" is a single word, and it has not the special chars, so it won't be quoted. If the value is number, it'll be just like number literal, but if the value is number string, it'll be quoted by double quoter.
In this time, the Dict4Ini support int, float, list/tuple, string, unicode data type, for others you should convert yourself.
import dict4ini
x = dict4ini.DictIni('test.ini')
print x.common.bool
print x.common.list
print x.common.name
So it's easy. The result will be:
1
[3, 1.2, 'Hello', 'have spaces']
limodou
The data is auto converted to its original type.
Many times, you may want to set default values of options, once it is not set in configuration file. Using Dict4Ini, you have many ways to do that:
x = dict4ini.DictIni()
x.test.setdefault('a', 'b')
print x.test.a
d = {'test':{'a':'b'}}
x = dict4ini.DictIni(values=d)
print x.test.a
x = dict4ini.DictIni()
d = {'a':'b'}
x.test = d
print x.test.a
import dict4ini
x = dict4ini.DictIni('test.ini')
x.common._comment = 'This is a comment test./nThis is the second line.'
x.common.name = 'limodou'
x.common.comment('name', 'Input your name')
x.common.bool = 1
x.common.comment('bool', 'Boot type')
x.common.list = ['3', 'Hello', 'have spaces']
x.common.comment('list', 'list testing')
x.save()
You can save comments in configuration also. Adding comments to section, you should using x.section._comment = 'comments'. Comments could be multi lines. Or you could use more commonly method, x.comment(). Just like x.comment('common', 'comments'). Add comments to options, you can only using comment() method, just like x.common.comment('list', 'comments').
The result of the ini file will be:
# This is a comment test.
# This is the second line.
[common]
# Boot type
bool = 1
# list testing
list = "3",Hello,"have spaces",
# Input your name
name = limodou
#coding=utf-8
import dict4ini
x = dict4ini.DictIni('test.ini')
x.common.name = u'中文名'
x.save()
Note: You should specify the coding used in the .py file. In this case is utf-8. Then I assign x.common.name with a unicode string. If you don't specify the encoding in create instance of the DictIni, the Dict4Ini will auto find the default encoding in the system in turns of:
* local.getdefaultlocale()[1]
* sys.getfilesystemencoding()
* utf-8
* The BOM of the file
The result of the ini file will be:
[common]
name = u"中文名"
You should notice the file encoding will be utf-8, and the name's value is like python unicode syntax. For easiness, it doesn't support using unicode in comments.
You can also specify the encoding of ini file, just like:
#coding=utf-8
import dict4ini
x = dict4ini.DictIni('test.ini', encoding='gbk')
x.common.name = u'中文名'
x.save()
It's easy to set an encoding of ini file.
import dict4ini
x = dict4ini.DictIni('test.ini')
x.common.settings.a = 1
x.common.settings.b = ["3", "hello"]
x.special.name = 'limodou'
x.special.homepage = 'http://www.donews.net/limodou'
x.save()
You don't need to care if subsection is created, you need to just use it.
The result of the ini file will be:
[common/settings]
a = 1
b = "3",hello,
[special]
homepage = http://www.donews.net/limodou
name = limodou
Sometimes we need to keep the order or the options according to the ini file, so how to get the ordered items?
ini = dict4ini.DictIni('x.ini')
for key, value in ini.ordereditems(ini):
print key, value
This example is dealing with the first level section.
ini = dict4ini.DictIni('x.ini')
for key, value in ini.ordereditems(ini.autostring):
print key, value
This example is dealing with certain section.
You can also encrypt some specified sections if you need. The Dict4Ini ships with a p3.py module, which can be used to encrypt/decrypt.
import dict4ini
d = dict4ini.DictIni('tt.ini', secretKey='limodou', secretSections='a')
d.a.name = 'limodou'
d.save()
And the result will be
[a]
name = v1ToGlTNo+YoB0VF5wk1Ea1XnRIVIv7xNNKOXSTdeA==
Here the parameter secretSections can be a string list or a string with ',' delimeter, for example: 'a,b' will be treated as 'b'. You should know all subsections of a specified section will be encrypted.
The code is:
import dict4ini
d = dict4ini.DictIni('tt.ini', hideData=True)
d.a.name = 'limodou'
d.save()
And the result is:
[a]
name = bGltb2RvdQ==
A: Yes. For example:
import dict4ini
x = dict4ini.DictIni('test.ini')
del x.a
x.save()
A: Easy. Just using dict syntax, for example:
x['common']['xxx.xxx'] = 'a'
or
x.common['xxx.xxx'] = 'a'
A: As you creating the DictIni instance, you can specify a "onelevel=True" parameter:
x = dict4ini.DictIni('inifile', onelevel=True)
But it'll not support multi section again. So you can also defined another sectiondelimeter char different from '/', just like:
x = dict4ini.DictIni('inifile', sectiondelimeter='@')
But every time you called dict4ini.DictIni() you may need including sectiondelimeter parameter.
A: Yes, it can. You can just simple pass a normal parameter to DictIni class. And all value will be treated string type, and when you saving the ini file, the value will be converted to string type automatically, and in normal mode, only these types support: int, float, str, unicode(will be automatically encoded to specifial encoding). And it doesn't support multiple level sections. And you need update to 0.9.3+.