日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Python配置文件解析模塊ConfigParser使用實例

系統 1769 0

一、ConfigParser簡介

ConfigParser 是用來讀取配置文件的包。配置文件的格式如下:中括號“[ ]”內包含的為section。section 下面為類似于key-value 的配置內容。

復制代碼 代碼如下:

?[db]
?db_host = 127.0.0.1
?db_port = 22
?db_user = root
?db_pass = rootroot
?
?[concurrent]
?thread = 10
?processor = 20

中括號“[ ]”內包含的為section。緊接著section 為類似于key-value 的options 的配置內容。
?
二、ConfigParser 初始工作

使用ConfigParser 首選需要初始化實例,并讀取配置文件:

復制代碼 代碼如下:

?cf = ConfigParser.ConfigParser()
?cf.read("配置文件名")

三、ConfigParser 常用方法

1. 獲取所有sections。也就是將配置文件中所有“[ ]”讀取到列表中:

復制代碼 代碼如下:

?s = cf.sections()
?print 'section:', s

將輸出(以下將均以簡介中配置文件為例):
復制代碼 代碼如下:

?section: ['db', 'concurrent']

2. 獲取指定section 的options。即將配置文件某個section 內key 讀取到列表中:
復制代碼 代碼如下:

?o = cf.options("db")
?print 'options:', o

將輸出:
復制代碼 代碼如下:

?options: ['db_host', 'db_port', 'db_user', 'db_pass']

3. 獲取指定section 的配置信息。
復制代碼 代碼如下:

?v = cf.items("db")
?print 'db:', v

將輸出:
復制代碼 代碼如下:

?db: [('db_host', '127.0.0.1'), ('db_port', '22'), ('db_user', 'root'), ('db_pass', 'rootroot')]

4. 按照類型讀取指定section 的option 信息。
同樣的還有getfloat、getboolean。
復制代碼 代碼如下:

?#可以按照類型讀取出來
?db_host = cf.get("db", "db_host")
?db_port = cf.getint("db", "db_port")
?db_user = cf.get("db", "db_user")
?db_pass = cf.get("db", "db_pass")
?
?# 返回的是整型的
?threads = cf.getint("concurrent", "thread")
?processors = cf.getint("concurrent", "processor")
?
?print "db_host:", db_host
?print "db_port:", db_port
?print "db_user:", db_user
?print "db_pass:", db_pass
?print "thread:", threads
?print "processor:", processors

將輸出:
復制代碼 代碼如下:

?db_host: 127.0.0.1
?db_port: 22
?db_user: root
?db_pass: rootroot
?thread: 10
?processor: 20

5. 設置某個option 的值。(記得最后要寫回)
復制代碼 代碼如下:

?cf.set("db", "db_pass", "zhaowei")
?cf.write(open("test.conf", "w"))

6.添加一個section。(同樣要寫回)
復制代碼 代碼如下:

?cf.add_section('liuqing')
?cf.set('liuqing', 'int', '15')
?cf.set('liuqing', 'bool', 'true')
?cf.set('liuqing', 'float', '3.1415')
?cf.set('liuqing', 'baz', 'fun')
?cf.set('liuqing', 'bar', 'Python')
?cf.set('liuqing', 'foo', '%(bar)s is %(baz)s!')
?cf.write(open("test.conf", "w"))

7. 移除section 或者option 。(只要進行了修改就要寫回的哦)
復制代碼 代碼如下:

?cf.remove_option('liuqing','int')
?cf.remove_section('liuqing')
?cf.write(open("test.conf", "w"))

復制代碼 代碼如下:

#!/usr/bin/env python
from ConfigParser import ConfigParser
CONFIGFILE="f.txt"
config=ConfigParser()
config.read(CONFIGFILE)
print config.get('messages','greeting')
radius=input(config.get('messages','questions')+' ')
print config.get('messages','result')
print config.getfloat('numbers','pi')*radius**2

s=config.sections()
print'section: ',s
o=config.options('messages')
print'messages option: ',o
v=config.items("messages")
print'message de xinxi: ',v

config.add_section('liuyang1')
config.set('liuyang1','int','15')
config.set('liuyang'1,'hhhh','hello world')
config.write(open("f.txt","w"))
print config.get('liuyang1','int')
print config.get('liuyang1','hhhh')


復制代碼 代碼如下:

#!/usr/bin/env python
import ConfigParser
import sys
config=ConfigParser.ConfigParser()
config.add_section("book1")
config.set("book1","title","hello world")
config.set("book1","aut","log")
config.write(open("f.txt","w"))


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。。?/p>

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 广德县| 巫山县| 翁牛特旗| 高台县| 浮山县| 大城县| 育儿| 鸡泽县| 吉隆县| 平度市| 阿克| 霍城县| 社旗县| 务川| 丰都县| 临江市| 罗田县| 娄烦县| 德保县| 女性| 阿城市| 清远市| 汉中市| 海阳市| 彭山县| 秦安县| 北宁市| 通榆县| 德清县| 宿迁市| 永清县| 大埔区| 陇南市| 宝丰县| 兴山县| 大庆市| 泾川县| 招远市| 昆明市| 宝丰县| 楚雄市|