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怎么读取excel中的数值

Python中数组,列表:冒号的用法介绍

Python对mysql数据库进行操作的实例详解

Python字典一个键只能有一个值吗

Python怎么看数据类型

Python中udp套接字通信的简单解析(附代码)

windows下Python连接oracle数据库实例方法

Python判断复数的基本步骤

为什么要设计好目录结构?

Python中if 条件判断代码解析

更多相关阅读请进入《Python》频道 >>




打赏

取消

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

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

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

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

评论

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