ConfigParser モジュールの詳細な紹介

零下一度
リリース: 2017-07-23 13:45:12
オリジナル
1538 人が閲覧しました

1. はじめに

共通の設定ドキュメントを生成および変更するために使用されます。Python 3.x バージョンでは、現在のモジュールの名前が configparser に変更されました。

2. 設定ファイルの形式

[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes
 
[bitbucket.org]
User = hg
 
[topsecret.server.com]
Port = 50022
ForwardX11 = no
ログイン後にコピー

3. 設定ファイルの作成

import configparser

# 生成一个处理对象
config = configparser.ConfigParser()  
#默认配置 
config["DEFAULT"] = {'ServerAliveInterval': '45',
                     'Compression': 'yes',
                     'CompressionLevel': '9'}

#生成其他的配置组
config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'

config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '50022'  # mutates the parser
topsecret['ForwardX11'] = 'no'  # same here

config['DEFAULT']['ForwardX11'] = 'yes'

#写入配置文件
with open('example.ini', 'w') as configfile:
    config.write(configfile)
ログイン後にコピー

4. 設定ファイルの読み取り

1. ノードの読み取り情報

りー

2. 構成ノード名が存在するかどうかを確認します

import configparser

config = configparser.ConfigParser()
config.read('example.ini')

# 读取默认配置节点信息
print(config.defaults())

#读取其他节点
print(config.sections())

# 输出
OrderedDict([('compression', 'yes'), ('serveraliveinterval', '45'), ('compressionlevel', '9'), ('forwardx11', 'yes')])

['bitbucket.org', 'topsecret.server.com']
ログイン後にコピー

3. 構成ノード内の情報を読み取ります

print('ssss' in config)
print('bitbucket.org' in config)

#输出
False
True
ログイン後にコピー

4. 構成ノードのすべての情報を読み取ります

print(config['bitbucket.org']['user'])

#输出

hg
ログイン後にコピー

以上がConfigParser モジュールの詳細な紹介の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!