日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python读取配置文件 ConfigParser

發(fā)布時(shí)間:2025/3/15 python 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python读取配置文件 ConfigParser 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
python讀取配置文件 ConfigParser

Python 標(biāo)準(zhǔn)庫(kù)的 ConfigParser 模塊提供一套 API 來讀取和操作配置文件。

?

配置文件的格式

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

b) section 用?[sect_name]?表示,每個(gè)option是一個(gè)鍵值對(duì),使用分隔符?=?或?:?隔開;

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

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

一個(gè)簡(jiǎn)單的配置文件樣例 myapp.conf

1 2 3 4 5 6 7 8 9 10 11 12 # 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) 實(shí)例化 ConfigParser 并加載配置文件

1 2 cp?=?ConfigParser.SafeConfigParser() cp.read('myapp.conf')

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

1 2 3 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) 讀取指定的配置信息

1 2 print?'host of db:', cp.get('db',?'host')?????# host of db: 127.0.0.1 print?'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 是否存在

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

f) 設(shè)置 option

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

g) 刪除 option

1 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 等操作并不會(huì)修改配置文件,write 方法可以將 ConfigParser 對(duì)象的配置寫到文件中

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

Unicode 編碼的配置

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

myapp.conf

[msg] hello = 你好

config_parser_unicode.py

import ConfigParser import 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 是一個(gè)鍵值對(duì)。但是,當(dāng)?SafeConfigParser 的參數(shù) allow_no_value 設(shè)置成 True 時(shí),它允許 option 不設(shè)置值而只是作為一個(gè)標(biāo)識(shí)。

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 默認(rèn)設(shè)置成 False,此時(shí)如果配置文件中存在沒有設(shè)置值的 option,在讀取配置文件時(shí)將拋出異常?ConfigParser.ParsingError。當(dāng)?allow_no_value 設(shè)置成 True 時(shí),如果一個(gè) option 沒有設(shè)置值,has_option 方法會(huì)返回 True,get 方法會(huì)返回 None。

?

DEFAULT section

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

db.conf

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

default_section.py

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

?

插值?Interpolation

SafeConfigParser 提供了插值的特性來結(jié)合數(shù)據(jù)。

url.conf

[DEFAULT] url = %(protocol)s://%(server)s:%(port)s/[http] protocol = http server = localhost port = 8080[ftp] url = %(protocol)s://%(server)s/ protocol = ftp server = 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 的使用,參考:

http://blog.csdn.net/zm2714/article/details/8002125

?

posted on 2017-07-12 11:40 秦瑞It行程實(shí)錄 閱讀(...) 評(píng)論(...) 編輯 收藏

轉(zhuǎn)載于:https://www.cnblogs.com/ruiy/p/7154616.html

總結(jié)

以上是生活随笔為你收集整理的python读取配置文件 ConfigParser的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。