1,ConfigParser模块简介
假设有如下配置文件,需要在Pyhton程序中读取
$ cat config.ini
[db]
db_port = 3306
db_user = root
db_host = 127.0.0.1
db_pass = xgmtest
[SectionOne]
Status: Single
Name: Derek
Value: Yes
Age: 30
Single: True
[SectionTwo]
FavoriteColor = Green
[SectionThree]
FamilyName: Johnson
[Others]
Route: 66
如何在Python中读取呢
>>> import ConfigParser
>>> Config = ConfigParser.ConfigParser()
>>> Config
<ConfigParser.ConfigParser instance at 0x00BA9B20>
>>> Config.read("config.ini")
['config.ini']
>>> Config.sections()
['db', 'Others', 'SectionThree', 'SectionOne', 'SectionTwo']
>>> Config.get("db", "db_host")
'127.0.0.1'
>>> Config.getint("db", "db_port")
3306
4 Comments