当前第2页 返回上一页
设定配置文件test2.conf
1 2 3 4 | [portal]
url = http: // %(host)s:%(port)s /Portal
host = localhost
port = 8080
|
使用RawConfigParser:
1 2 3 4 5 6 7 8 9 10 11 | import ConfigParser
cf = ConfigParser.RawConfigParser()
print "use RawConfigParser() read"
cf.read( "test2.conf" )
print cf.get( "portal" , "url" )
print "use RawConfigParser() write"
cf. set ( "portal" , "url2" , "%(host)s:%(port)s" )
print cf.get( "portal" , "url2" )
|
得到终端输出:
1 2 3 4 | use RawConfigParser() read
http: // %(host)s:%(port)s /Portal
use RawConfigParser() write
%(host)s:%(port)s
|
改用ConfigParser:
1 2 3 4 5 6 7 8 9 10 11 | import ConfigParser
cf = ConfigParser.ConfigParser()
print "use ConfigParser() read"
cf.read( "test2.conf" )
print cf.get( "portal" , "url" )
print "use ConfigParser() write"
cf. set ( "portal" , "url2" , "%(host)s:%(port)s" )
print cf.get( "portal" , "url2" )
|
得到终端输出:
1 2 3 4 | use ConfigParser() read
http: //localhost :8080 /Portal
use ConfigParser() write
localhost:8080
|
改用SafeConfigParser:
1 2 3 4 5 6 7 8 9 10 11 | import ConfigParser
cf = ConfigParser.SafeConfigParser()
print "use SafeConfigParser() read"
cf.read( "test2.conf" )
print cf.get( "portal" , "url" )
print "use SateConfigParser() write"
cf. set ( "portal" , "url2" , "%(host)s:%(port)s" )
print cf.get( "portal" , "url2" )
|
得到终端输出(效果同ConfigParser):
1 2 3 4 | use SafeConfigParser() read
http: //localhost :8080 /Portal
use SateConfigParser() write
localhost:8080
|
以上就是Python之ConfigParser配置文件详解的详细内容,更多文章请关注木庄网络博客!!
返回前面的内容
相关阅读 >>
浅谈Python字符串
Python中如何从列表中删除none值
Python常用函数有哪些
Python的idle是什么
Python有double类型吗
Python怎么念
Python压缩包怎么安装
Python怎么读?
常用Python解释器有哪些
Python中表达式4+0.5值的数据类型为?
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » Python之ConfigParser配置文件详解