欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Python中怎么利用ConfigParser解析配置模塊

這篇文章將為大家詳細(xì)講解有關(guān)Python 中怎么利用ConfigParser解析配置模塊,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

創(chuàng)新互聯(lián)公司服務(wù)項(xiàng)目包括神農(nóng)架林區(qū)網(wǎng)站建設(shè)、神農(nóng)架林區(qū)網(wǎng)站制作、神農(nóng)架林區(qū)網(wǎng)頁制作以及神農(nóng)架林區(qū)網(wǎng)絡(luò)營(yíng)銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,神農(nóng)架林區(qū)網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到神農(nóng)架林區(qū)省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

1.基本的讀取配置文件

-read(filename) 直接讀取ini文件內(nèi)容

-sections() 得到所有的section,并以列表的形式返回

-options(section) 得到該section的所有option

-items(section) 得到該section的所有鍵值對(duì)

-get(section,option) 得到section中option的值,返回為string類型

-getint(section,option) 得到section中option的值,返回為int類型,還有相應(yīng)的getboolean()和getfloat() 函數(shù)。

2.基本的寫入配置文件

-add_section(section) 添加一個(gè)新的section

-set( section, option, value) 對(duì)section中的option進(jìn)行設(shè)置,需要調(diào)用write將內(nèi)容寫入配置文件。

3.基本例子

test.conf

1

2

3

4

5

6

7

8

9

[sec_a] 

a_key1 = 20 

a_key2 = 10 

   

[sec_b] 

b_key1 = 121 

b_key2 = b_value2 

b_key3 = $r 

b_key4 = 127.0.0.1

parse_test_conf.py

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

import ConfigParser 

cf = ConfigParser.ConfigParser() 

#read config 

cf.read("test.conf") 

# return all section 

secs = cf.sections() 

print 'sections:', secs 

   

opts = cf.options("sec_a") 

print 'options:', opts 

   

kvs = cf.items("sec_a") 

print 'sec_a:', kvs 

   

#read by type 

str_val = cf.get("sec_a", "a_key1") 

int_val = cf.getint("sec_a", "a_key2") 

   

print "value for sec_a's a_key1:", str_val 

print "value for sec_a's a_key2:", int_val 

   

#write config 

#update value 

cf.set("sec_b", "b_key3", "new-$r") 

#set a new value 

cf.set("sec_b", "b_newkey", "new-value") 

#create a new section 

cf.add_section('a_new_section') 

cf.set('a_new_section', 'new_key', 'new_value') 

   

#write back to configure file 

cf.write(open("test.conf", "w"))

得到終端輸出:

1

2

3

4

5

sections: ['sec_b', 'sec_a'] 

options: ['a_key1', 'a_key2'] 

sec_a: [('a_key1', "i'm value"), ('a_key2', '22')] 

value for sec_a's a_key1: i'm value 

value for sec_a's a_key2: 22

更新后的test.conf

1

2

3

4

5

6

7

8

9

10

11

12

13

[sec_b] 

b_newkey = new-value 

b_key4 = 127.0.0.1 

b_key1 = 121 

b_key2 = b_value2 

b_key3 = new-$r 

   

[sec_a] 

a_key1 = i'm value 

a_key2 = 22 

   

[a_new_section] 

new_key = new_value

4.Python的ConfigParser Module中定義了3個(gè)類對(duì)INI文件進(jìn)行操作。分別是RawConfigParser、ConfigParser、SafeConfigParser。RawCnfigParser是最基礎(chǔ)的INI文件讀取類,ConfigParser、SafeConfigParser支持對(duì)%(value)s變量的解析。 

設(shè)定配置文件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

關(guān)于Python 中怎么利用ConfigParser解析配置模塊就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

當(dāng)前名稱:Python中怎么利用ConfigParser解析配置模塊
標(biāo)題URL:http://chinadenli.net/article18/jijegp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄域名注冊(cè)微信小程序微信公眾號(hào)網(wǎng)站策劃網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作