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

首頁 > 編程 > Python > 正文

Python ConfigParser 注意事項

2019-11-06 06:56:28
字體:
來源:轉載
供稿:網友

Python ConfigParser 注意事項

以這個非常簡單的典型配置文件為例:

[DEFAULT]ServerAliveInterval = 45ComPRession = yesCompressionLevel = 9ForwardX11 = yes[bitbucket.org]User = hg[topsecret.server.com]Port = 50022ForwardX11 = no

1、config parser 操作跟dict 類似,在數據存取方法基本一致

>> import configparser>>> config = configparser.ConfigParser()>>> config.sections()[]>>> config.read('example.ini')['example.ini']>>> config.sections()['bitbucket.org', 'topsecret.server.com']>>> 'bitbucket.org' in configTrue>>> 'bytebong.com' in configFalse>>> config['bitbucket.org']['User']'hg'>>> config['DEFAULT']['Compression']'yes'>>> topsecret = config['topsecret.server.com']>>> topsecret['ForwardX11']'no'>>> topsecret['Port']'50022'>>> for key in config['bitbucket.org']: print(key)...usercompressionlevelserveraliveintervalcompressionforwardx11>>> config['bitbucket.org']['ForwardX11']'yes'2、默認配置項[DEFAULT]section 的默認參數會作用于其他Sections3、數據類型 config parsers 不會猜測或自動分析識別config.ini參數的數據類型,都會按照字符串類型存儲,如果需要讀取為其他數據類型,需要自定義轉換。 特殊bool值:對于常見的布爾值’yes’/’no’, ‘on’/’off’, ‘true’/’false’ 和 ‘1’/’0’,提供了getboolean()方法。4、獲取參數值方法 get() 使用get()方法獲取每一參數項的配置值。 如果一般Sections 中參數在[DEFAULT]中也有設置,則get()到位[DEFAULT]中的參數值。5、參數分隔符可以使用‘=’或‘:’(默認)6、可以使用‘#’或‘;’(默認)添加備注或說明[Simple Values]key=valuespaces in keys=allowedspaces in values=allowed as wellspaces around the delimiter = obviouslyyou can also use : to delimit keys from values[All Values Are Strings]values like this: 1000000or this: 3.14159265359are they treated as numbers? : nointegers, floats and booleans are held as: stringscan use the API to get converted values directly: true[Multiline Values]chorus: I'm a lumberjack, and I'm okay I sleep all night and I work all day[No Values]key_without_valueempty string value here =[You can use comments]# like this; or this# By default only in an empty line.# Inline comments can be harmful because they prevent users# from using the delimiting characters as parts of values.# That being said, this can be customized. [Sections Can Be Indented] can_values_be_as_well = True does_that_mean_anything_special = False purpose = formatting for readability multiline_values = are handled just fine as long as they are indented deeper than the first line of a value # Did I mention we can indent comments, too?7、寫配置

常見做法:

config.write(open('example.ini', 'w'))

合理做法:

with open('example.ini', 'w') as configfile: config.write(configfile)

待續。。。。。。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 富源县| 浪卡子县| 五常市| 新兴县| 全州县| 汤阴县| 青冈县| 腾冲县| 平定县| 高唐县| 左云县| 武胜县| 澄迈县| 临高县| 鄂托克前旗| 吉木乃县| 万安县| 蒲江县| 旌德县| 阿拉尔市| 许昌市| 称多县| 花莲县| 民丰县| 新昌县| 平潭县| 滨海县| 叶城县| 凤冈县| 旬阳县| 昭通市| 广饶县| 庆元县| 札达县| 云安县| 游戏| 新乡市| 永顺县| 渝中区| 兴海县| 八宿县|