パイソンのConfigParserモジュール(プロファイル)

pythonのConfigParserモジュール

ファイルがあり、コンフィギュレーション・ファイルを使用する必要があります

[userinfoを] 

ユーザ名 = 
パスワード = PWD

 

インポートのConfigParser 
コンフィグ = configparser.ConfigParser()
コンフィグ[ " DEFAULT " ] = { ' ServerAliveInterval '' 45 ' ' 圧縮'' はい' ' がCompressionLevel '' 9 ' ' ForwardX11 '' はい' 
                     } 
コンフィグ[ "bitbucket.org ' ] = { ' ユーザ'' HG ' } 

コンフィグ[ ' topsecret.server.com ' ] = { ' ホストポート'' 50022 ' ' ForwardX11 '' いいえ' } 

オープンと(' 例。 INI ' ' W ' Fとして):
   config.write(F)

 

インポートのConfigParser 

コンフィグ = configparser.ConfigParser()
---------------------------辞書の形式に基づいて、文書の内容を見つける
#の印刷(設定。セクション())#[] 

config.read(' example.ini ' プリント(config.sections())           [ 'bitbucket.org'、 'topsecret.server.com'] 

プリント' bytebong.com '  設定)
印刷' bitbucket.org '  設定)

印刷(設定[ "bitbucket.org ' ] [ " ユーザー" ])  HGの
印刷(設定[ ' DEFAULT ' ] [ ' 圧縮' ]) はい
プリント(設定[ ' topsecret.server.com ' ] [ ' ForwardX11 " ])  無し

印刷(設定[ ' bitbucket.org ' ])          <セクション:bitbucket.org> 

のためのキー設定[ ' bitbucket.org ']:      #1 デフォルトデフォルトデフォルトのキーがあることを注意
    印刷は、(キー)

を印刷(config.options(bitbucket.org ))  forループで、見つける「bitbucket.org」すべてのキー

を印刷(config.items(" bitbucket.org ))    検索」「すべてのキーと値のペアbitbucket.org 

印刷(config.get(bitbucket.org 圧縮"))#1 節の方法で主イエス対応する値を取得します

 

インポートのConfigParser 
コンフィグ = configparser.ConfigParser()
config.read(' example.ini '#は、ファイルの読み込み 
config.add_section(' '#は、セクションの増加 
config.remove_section(' bitbucket.org '#は、セクションの削除 
config.remove_optionを(' topsecret.server.com '" ForwardX11 "#は、構成アイテムの削除 
(config.setを' topsecret.server.com '"K1 ' ' 11111 ' 
config.set(' ' ' K2 ' ' 22222 ' 
、F =オープン(' new2.ini " W " 
config.write(F) 写进文件 
f.close ()

 

おすすめ

転載: www.cnblogs.com/yrash/p/11319357.html