<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # configparser 模塊 > 本文由 [簡悅 SimpRead](http://ksria.com/simpread/) 轉碼, 原文地址 https://blog.csdn.net/shortwall/article/details/78615368 版權聲明:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/shortwall/article/details/78615368 ## configparser 簡介 configparser 是 Pyhton 標準庫中用來解析配置文件的模塊,并且內置方法和字典非常接近。Python2.x 中名為 ConfigParser,3.x 已更名小寫,并加入了一些新功能。 配置文件的格式如下: ```python [DEFAULT] ServerAliveInterval = 45 Compression = yes CompressionLevel = 9 ForwardX11 = yes [bitbucket.org] User = Tom [topsecret.com] Port: 50022 ForwardX11: no ``` “[]” 包含的為 section,section 下面為類似于 key - value 的配置內容; configparser 默認支持 ‘=’ ‘:’ 兩種分隔。 * * * ## configparser 常用方法 ### 初始化實例 使用 configparser 首先需要初始化實例,并讀取配置文件: ```python >>> import configparser >>> config = configparser.ConfigParser() # 注意大小寫 >>> config.read("config.ini") # 配置文件的路徑 ["config.ini"] ``` 或者可以直接讀字典 ```python >>> parser = configparser.ConfigParser() >>> parser.read_dict({'section1': {'key1': 'value1', ... 'key2': 'value2', ... 'key3': 'value3'}, ... 'section2': {'keyA': 'valueA', ... 'keyB': 'valueB', ... 'keyC': 'valueC'}, ... 'section3': {'foo': 'x', ... 'bar': 'y', ... 'baz': 'z'} ... }) ``` ### 獲取所有 sections ```python >>> config.sections() ['bitbucket.org', 'topsecret.com'] # 注意會過濾掉[DEFAULT] ``` ### 獲取指定 section 的 keys & values ```python >>> config.items('topsecret.com') >>>> [('port', '50022'), ('forwardx11', 'no')] # 注意items()返回的字符串會全變成小寫 ``` ### 獲取指定 section 的 keys ```python >>> config.options('topsecret.com') ['Port', 'ForwardX11'] ``` ```python >>> for option in config['topsecret.com']: ... print(option) Port ForwardX11 ``` ### 獲取指定 key 的 value ```python >>> config['bitbucket.org']['User'] 'Tom' ``` ```python >>> config.get('bitbucket.org', 'User') 'Tom' >>> config.getint('topsecret.com', 'Port') 50022 ``` ### 檢查 ```python >>> 'DEFAULT' in config True >>> 'test' in config['section_test'] False >>> 'Tom' in config['bitbucket.org']['User'] True ``` ```python >>> config.has_section('bitbucket.org') True >>> config.has_option('section_test', 'test') False ``` ### 添加 ```python >>> config.add_section('Section_1') >>> config.set('Section_1', 'key_1', 'value_1') # 注意鍵值是用set()方法 >>> config.write(open('config.ini', 'w')) # 一定要寫入才生效 ``` ### 刪除 ```python >>> config.remove_option('Section_1', 'key_1') True >>> config.remove_section('Section_1') True >>> config.clear() # 清空除[DEFAULT]之外所有內容 >>> config.write(open('config.ini', 'w')) ``` * * * ## 關于 [DEFAULT] [DEFAULT] 一般包含 ini 格式配置文件的默認項,所以 configparser 部分方法會自動跳過這個 section 。 前面已經提到 sections() 是獲取不到的,還有刪除方法對 [DEFAULT] 也無效: ```python >>> config.remove_section('DEFAULT') False >>> config.clear() >>> 'DEFAULT' in config True >>> 'ForwardX11' in config['DEFAULT'] True >>> config.sections() [] ``` 但指定刪除和修改 [DEFAULT] 里的 keys & values 是可以的: ```python >>> config.remove_option('DEFAULT', 'ForwardX11') True >>> config.set('DEFAULT', 'ForwardX11','no') >>> config['DEFAULT']['ForwardX11'] 'no' ``` 還有個特殊的是,has_section() 也無效,可以和 in 區別使用 ```python >>> config.has_section('DEFAULT') False >>> 'DEFAULT' in config True ``` > 更多用法請看官方文檔:[https://docs.python.org/3.6/library/configparser.html](https://docs.python.org/3.6/library/configparser.html) <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/markdown_views-ea0013b516.css"> <sr-plugin-count>共計:3087 個字</sr-plugin-count>
                  <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>

                              哎呀哎呀视频在线观看