国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Python(2.7.6)ConfigParser-讀寫配置文件

2019-11-14 17:20:52
字體:
來源:轉載
供稿:網友

Python 標準庫的 ConfigParser 模塊提供一套 API 來讀取和操作配置文件。

 

配置文件的格式

a) 配置文件中包含一個或多個 section, 每個 section 有自己的 option;

b) section 用 [sect_name] 表示,每個option是一個鍵值對,使用分隔符 =: 隔開;

c) 在 option 分隔符兩端的空格會被忽略掉

d) 配置文件使用 # 和 ; 注釋

一個簡單的配置文件樣例 myapp.conf

# database source
[db]
host = 127.0.0.1
port = 3306
user = root
pass = root

# ssh
[ssh]
host = 192.168.1.101
user = huey
pass = huey

 

ConfigParser 的基本操作

a) 實例化 ConfigParser 并加載配置文件

cp = ConfigParser.SafeConfigParser()cp.read('myapp.conf')

b) 獲取 section 列表、option 鍵列表和 option 鍵值元組列表 

PRint 'all sections:', cp.sections()        # sections: ['db', 'ssh']print 'options of [db]:', cp.options('db')  # options of [db]: ['host', 'port', 'user', 'pass']print 'items of [ssh]:', cp.items('ssh')    # items of [ssh]: [('host', '192.168.1.101'), ('user', 'huey'), ('pass', 'huey')]

c) 讀取指定的配置信息

print 'host of db:', cp.get('db', 'host')     # host of db: 127.0.0.1print 'host of ssh:', cp.get('ssh', 'host')   # host of ssh: 192.168.1.101

d) 按類型讀取配置信息:getint、 getfloat 和 getboolean

print type(cp.getint('db', 'port'))        # <type 'int'>

e) 判斷 option 是否存在

print cp.has_option('db', 'host')    # True

f) 設置 option

cp.set('db', 'host','192.168.1.102')

g) 刪除 option

cp.remove_option('db', 'host')

h) 判斷 section 是否存在

print cp.has_section('db')    # True

i) 添加 section

cp.add_section('new_sect')

j) 刪除 section

cp.remove_section('db')

k) 保存配置,set、 remove_option、 add_section 和 remove_section 等操作并不會修改配置文件,write 方法可以將 ConfigParser 對象的配置寫到文件中

cp.write(open('myapp.conf', 'w'))cp.write(sys.stdout)

 

Unicode 編碼的配置

配置文件如果包含 Unicode 編碼的數據,需要使用 codecs 模塊以合適的編碼打開配置文件。

myapp.conf

[msg]hello = 你好

config_parser_unicode.py

import ConfigParserimport codecscp = ConfigParser.SafeConfigParser()with codecs.open('myapp.conf', 'r', encoding='utf-8') as f:    cp.readfp(f)print cp.get('msg', 'hello')

 

allow_no_value

通常情況下, option 是一個鍵值對。但是,當 SafeConfigParser 的參數 allow_no_value 設置成 True 時,它允許 option 不設置值而只是作為一個標識。

allow_no_value.conf

# option as Flag
[flag]
flag_opt

allow_no_value.py

import ConfigParsercp = ConfigParser.SafeConfigParser(allow_no_value = True)cp.read('myapp.conf')print cp.get('flag', 'flag_opt');    # None

allow_no_value 默認設置成 False,此時如果配置文件中存在沒有設置值的 option,在讀取配置文件時將拋出異常 ConfigParser.ParsingError。當 allow_no_value 設置成 True 時,如果一個 option 沒有設置值,has_option 方法會返回 True,get 方法會返回 None。

 

DEFAULT section

如果配置文件中存在一個名為 DEFAULT 的 section,那么其他 section 會擴展它的 option 并且可以覆蓋它的 option。

db.conf

[DEFAULT]host = 127.0.0.1port = 3306[db_root]user = rootpass = root[db_huey]host = 192.168.1.101user = hueypass = huey

default_section.py

print cp.get('db_root', 'host')    # 127.0.0.1print cp.get('db_huey', 'host')    # 192.168.1.101

 

插值 Interpolation

SafeConfigParser 提供了插值的特性來結合數據。

url.conf

[DEFAULT]url = %(protocol)s://%(server)s:%(port)s/[http]protocol = httpserver = localhostport = 8080[ftp]url = %(protocol)s://%(server)s/protocol = ftpserver = 192.168.1.102

interpolation_demo.py

import ConfigParsercp = ConfigParser.SafeConfigParser()cp.read('url.conf')print cp.get('http', 'url')    # http://localhost:8080/print cp.get('ftp', 'url')     # ftp://192.168.1.102/

 

更多 ConfigParser 的使用,參考:

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 高陵县| 环江| 东至县| 饶河县| 六安市| 桂平市| 共和县| 色达县| 阜城县| 华坪县| 盱眙县| 恩施市| 绿春县| 通州区| 社旗县| 和静县| 玉树县| 梧州市| 浦县| 陆丰市| 会昌县| 岢岚县| 庆城县| 鄱阳县| 新晃| 泰顺县| 汕头市| 顺昌县| 南部县| 藁城市| 赞皇县| 神池县| 乌拉特前旗| 沈丘县| 额尔古纳市| 屏东县| 临沂市| 裕民县| 恭城| 安康市| 济阳县|