激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語言編程技術(shù)及教程分享平臺!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - Python中的ConfigParser模塊使用詳解

Python中的ConfigParser模塊使用詳解

2020-06-20 10:25腳本之家 Python

這篇文章主要介紹了Python中的ConfigParser模塊的使用,ConfigParser模塊主要被用來讀寫配置模塊,需要的朋友可以參考下

1.基本的讀取配置文件

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

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

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

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

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

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

2.基本的寫入配置文件

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

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

 

3.基本例子

?
1
2
3
4
5
6
7
8
9
10
test.conf
[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
?
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
33
parse_test_conf.py
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個類對INI文件進行操作。分別是RawConfigParser、ConfigParser、SafeConfigParser。RawCnfigParser是最基礎(chǔ)的INI文件讀取類,ConfigParser、SafeConfigParser支持對%(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

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25
主站蜘蛛池模板: 福利在线播放 | 欧美精品久久久久久久久久 | 国产小视频一区 | 亚洲国产精品久久久久久久久久久 | 国产精品久久久久久婷婷天堂 | 欧美成人小视频 | 九九热在线视频免费观看 | 国产精品伦视频看免费三 | 成人免费区 | 国产精品自拍av | 午夜精品影院 | 羞羞视频免费视频欧美 | 亚洲日本欧美 | 视频一区二区不卡 | 国产成人精品免费视频大全办公室 | 亚洲欧美日韩一区二区三区在线观看 | 日韩中文字幕三区 | 黄色高清免费网站 | 欧美成人做爰高潮片免费视频 | av电影免费在线看 | 国产成人强伦免费视频网站 | 国产精品999在线观看 | 最新福利在线 | 亚洲一区二区三区在线 | hd性videos意大利复古 | 精品国产一区二区三区成人影院 | 欧美一级美国一级 | 99激情 | 国内精品久久久久久2021浪潮 | 亚洲精品aaaaa| 精品人伦一区二区三区蜜桃网站 | 中文欧美日韩 | 日本中文高清 | 国产99久久久国产精品下药 | 久久9久久 | 农村少妇吞精夜夜爽视频 | 成人午夜毛片 | 最新日韩在线观看视频 | 国产精品视频免费网站 | 日韩在线黄 | 高清中文字幕在线 |