Python之ConfigParser配置文件详解


当前第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》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...