久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

Python configparser模塊配置文件過程解析

瀏覽:128日期:2022-08-04 15:40:49

ConfigParser模塊在Python3修改為configparser,這個模塊定義了一個ConfigeParser類,該類的作用是讓配置文件生效。配置文件的格式和window的ini文件相同

編輯配置文件: .ini

模板:內容自定義

一、 編輯配置文件

import configparserconfig = configparser.ConfigParser()config[’DEFAULT’] = { ’ServerAliveInterval’:’45’, ’Compression’:’yes’, ’CompressionLevel’:’9’, ’ForwardX11’:’yes’}config[’bitbucker.org’] = { ’Host Port’:’50022’, ’ForwardX11’:’no’}config[’path’] = { ’Base_Path’:’D:pythonpychrom路飛學城day8’, ’student_path’:’D:pythonpychrom路飛學城day8configparser模塊.py’}with open(’example.ini’,’w’,encoding=’utf-8’) as configfile: config.write(configfile)

二、讀取配置文件

import configparserconfig = configparser.ConfigParser()config.read(’example.ini’,encoding=’utf-8’)print(config.sections()) # 查看分組情況,默認default是不顯示的print(’bitbucker.org’ in config) # Flase 判斷一個組在不在這個文件當中print(’bitbucker.com’ in config) # Trueprint(config[’bitbucker.org’][’host_port’]) # 查釗這個文件中這個分組下面有沒有這個配置print(config[’bitbucker.org’][’user’]) # 沒有就報錯for key in config[’bitbucker.org’]: # 取默認分組和這個組的下面所有配置 print(key)# 只能取到 keyprint(config.options(’bitbucker.org’)) # 取分組下面的配置,包括默認分組 只能取到值print(config.items(’bitbucker.org’)) # 取到分組下面的鍵值對,包括默認分組print(config.get(’path’,’base_path’)) # 獲取某個分組下面的鍵來獲取值

三、增刪改查

import configparserconfig = configparser.ConfigParser()config.read(’example.ini’,encoding=’utf-8’)config.add_section(’zuming’) # 添加組config.remove_section(’zuming’) # 刪除一個組config.remove_option(’bitbucker.org’,’host_port’) # 刪除某個組中的某一項config.set(’bitbucker.org’,’host_port’,’22’) # 修改某個組下面的值config.write(open(’example.ini’,’w’,encoding=’utf-8’)) # 必須添加這句話才能生效

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 延安市| 岗巴县| 蒲城县| 和田县| 望城县| 雷波县| 三穗县| 太原市| 万山特区| 攀枝花市| 五大连池市| 永昌县| 紫金县| 甘南县| 精河县| 汝州市| 鄂托克旗| 来凤县| 桃园市| 炎陵县| 上思县| 离岛区| 闵行区| 金沙县| 昌黎县| 甘南县| 保德县| 天长市| 齐河县| 合阳县| 永福县| 义马市| 镇平县| 吉水县| 确山县| 沽源县| 兴安盟| 永新县| 南召县| 仁怀市| 布尔津县|