<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Python `ConfigParser`教程 > 原文: [http://zetcode.com/python/configparser/](http://zetcode.com/python/configparser/) Python `ConfigParser`教程顯示了如何使用`ConfigParser`在 Python 中使用配置文件。 ## Python `ConfigParser` `ConfigParser`是一個 Python 類,為 Python 程序實現基本的配置語言。 它提供類似于 Microsoft Windows INI 文件的結構。 `ConfigParser`允許編寫可由最終用戶輕松定制的 Python 程序。 配置文件由各部分組成,后跟選項的鍵/值對。 段名用`[]`字符分隔。 這些對用`:`或`=`隔開。 注釋以`#`或`;`開頭。 ## Python `ConfigParser`讀取文件 在第一個示例中,我們從文件中讀取配置數據。 `db.ini` ```py [mysql] host = localhost user = user7 passwd = s$cret db = ydb [postgresql] host = localhost user = user8 passwd = mypwd$7 db = testdb ``` 我們有兩部分配置數據。 `reading_from_file.py` ```py #!/usr/bin/env python3 import configparser config = configparser.ConfigParser() config.read('db.ini') host = config['mysql']['host'] user = config['mysql']['user'] passwd = config['mysql']['passwd'] db = config['mysql']['db'] print('MySQL configuration:') print(f'Host: {host}') print(f'User: {user}') print(f'Password: {passwd}') print(f'Database: {db}') host2 = config['postgresql']['host'] user2 = config['postgresql']['user'] passwd2 = config['postgresql']['passwd'] db2 = config['postgresql']['db'] print('PostgreSQL configuration:') print(f'Host: {host2}') print(f'User: {user2}') print(f'Password: {passwd2}') print(f'Database: {db2}') ``` 該示例讀取 MySQL 和 PostgreSQL 的配置數據。 ```py config = configparser.ConfigParser() config.read('db.ini') ``` 我們啟動`ConfigParser`并使用`read()`讀取文件。 ```py host = config['mysql']['host'] user = config['mysql']['user'] passwd = config['mysql']['passwd'] db = config['mysql']['db'] ``` 我們從 mysql 部分訪問選項。 ```py host2 = config['postgresql']['host'] user2 = config['postgresql']['user'] passwd2 = config['postgresql']['passwd'] db2 = config['postgresql']['db'] ``` 我們從 postgresql 部分訪問選項。 ```py $ python reading_from_file.py MySQL configuration: Host: localhost User: user7 Password: s$cret Database: ydb PostgreSQL configuration: Host: localhost User: user8 Password: mypwd$7 Database: testdb ``` 這是輸出。 ## Python `ConfigParser`部分 配置數據分為幾部分。 `sections()`讀取所有部分,`has_section()`檢查是否存在指定的部分。 `sections.py` ```py #!/usr/bin/env python3 import configparser config = configparser.ConfigParser() config.read('db.ini') sections = config.sections() print(f'Sections: {sections}') sections.append('sqlite') for section in sections: if config.has_section(section): print(f'Config file has section {section}') else: print(f'Config file does not have section {section}') ``` 該示例適用于各節。 ```py $ python sections.py Sections: ['mysql', 'postgresql'] Config file has section mysql Config file has section postgresql Config file does not have section sqlite ``` 這是輸出。 ## Python `ConfigParser`從字符串讀取 從 Python 3.2 開始,我們可以使用`read_string()`方法從字符串讀取配置數據。 `read_from_string.py` ```py #!/usr/bin/env python3 import configparser cfg_data = ''' [mysql] host = localhost user = user7 passwd = s$cret db = ydb ''' config = configparser.ConfigParser() config.read_string(cfg_data) host = config['mysql']['host'] user = config['mysql']['user'] passwd = config['mysql']['passwd'] db = config['mysql']['db'] print(f'Host: {host}') print(f'User: {user}') print(f'Password: {passwd}') print(f'Database: {db}') ``` 該示例從字符串讀取配置。 ## Python `ConfigParser`從字典中讀取 從 Python 3.2 開始,我們可以使用`read_dict()`方法從字典中讀取配置數據。 `read_from_dict.py` ```py #!/usr/bin/env python3 import configparser cfg_data = { 'mysql': {'host': 'localhost', 'user': 'user7', 'passwd': 's$cret', 'db': 'ydb'} } config = configparser.ConfigParser() config.read_dict(cfg_data) host = config['mysql']['host'] user = config['mysql']['user'] passwd = config['mysql']['passwd'] db = config['mysql']['db'] print(f'Host: {host}') print(f'User: {user}') print(f'Password: {passwd}') print(f'Database: {db}') ``` 該示例從 Python 字典讀取配置。 ```py cfg_data = { 'mysql': {'host': 'localhost', 'user': 'user7', 'passwd': 's$cret', 'db': 'ydb'} } ``` 鍵是部分名稱,值是帶有該部分中存在的鍵和值的字典。 ## Python `ConfigParser`寫入 `write()`方法寫入配置數據。 `writing.py` ```py #!/usr/bin/env python3 import configparser config = configparser.ConfigParser() config.add_section('mysql') config['mysql']['host'] = 'localhost' config['mysql']['user'] = 'user7' config['mysql']['passwd'] = 's$cret' config['mysql']['db'] = 'ydb' with open('db3.ini', 'w') as configfile: config.write(configfile) ``` 該示例將配置數據寫入`db3.ini`文件。 ```py config.add_section('mysql') ``` 首先,我們用`add_section()`添加一個部分。 ```py config['mysql']['host'] = 'localhost' config['mysql']['user'] = 'user7' config['mysql']['passwd'] = 's$cret' config['mysql']['db'] = 'ydb' ``` 然后我們設置選項。 ```py with open('db3.ini', 'w') as configfile: config.write(configfile) ``` 最后,我們用`write()`寫入數據。 ## Python `ConfigParser`插值 `ConfigParser`允許在配置文件中使用插值。 它使用`%()`語法。 `cfg.ini` ```py [info] users_dir= C:\Users name= Jano home_dir= %(users_dir)s\%(name)s ``` 我們用插值法構建`home_dir`。 請注意,`"s"`字符是語法的一部分。 `interpolation.py` ```py #!/usr/bin/env python3 import configparser config = configparser.ConfigParser() config.read('cfg.ini') users_dir = config['info']['users_dir'] name = config['info']['name'] home_dir = config['info']['home_dir'] print(f'Users directory: {users_dir}') print(f'Name: {name}') print(f'Home directory: {home_dir}') ``` 該示例讀取值并打印出來。 ```py $ python interpolation.py Users directory: C:\Users Name: Jano Home directory: C:\Users\Jano ``` 這是輸出。 在本教程中,我們使用`ConfigParser`處理 Python 中的配置數據。 您可能也會對以下相關教程感興趣: [Python 教程](/lang/python/), [Python 哈希教程](/python/hashing/)或列出[所有 Python 教程](/all/#python)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看