国产探花免费观看_亚洲丰满少妇自慰呻吟_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)

待續。。。。。。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 衡阳县| 土默特右旗| 武强县| 瑞丽市| 金塔县| 喀喇| 枣阳市| 大余县| 虞城县| 东平县| 上饶县| 西安市| 磐石市| 青岛市| 张掖市| 扎兰屯市| 商河县| 景宁| 宁化县| 霍山县| 乐亭县| 沁源县| 丽江市| 武隆县| 翼城县| 莎车县| 青川县| 罗江县| 大荔县| 尚义县| 北辰区| 丰城市| 白山市| 光泽县| 上栗县| 繁峙县| 周口市| 玉屏| 福州市| 榆林市| 安宁市|