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

您的位置:首頁技術(shù)文章
文章詳情頁

Python ini文件常用操作方法解析

瀏覽:4日期:2022-07-27 13:50:42

一、config.ini 配置文件

[DATABASE]host = 192.1.1.1username = rootpassword = rootport = 3306database = jforum

[URL]#ip,端口ip =127.0.0.1port= 8089

二、操作ini常用方法

--read():讀取配置文件--sections():讀取配置文件中所有的section(如上配置文件:DATABASE,URL)--options(section):讀取該section下所有的option(可以理解成讀取該組下的所有key,如options('URL'),值[’ip’, ’port’])--items(section):讀取該section下的所有key-vaule,并以鍵值對形式輸出(如:sectioitems('URL'),值:[(’ip’, ’127.0.0.1’), (’port’, ’8089’)])--get(section, option):讀取指定section下面的option的值(可以理解成,讀取具體某個(gè)section下面指定key的值,如config.get(’URL’,’ip’)),值:127.0.0.1)--add_section(section):添加一個(gè)section,參數(shù)為section的名稱--set(section, option, value):在section下面添加一條數(shù)據(jù)(key=value)--add與set需調(diào)用write(open(configPath, 'a'))才可以寫入ini文件 #參數(shù)a表示最近,w重寫--remove_seciton(seciton) 刪除整個(gè)seciton--config.remove_option(seciton,key) ,刪除seciton的某個(gè)key值

三、源碼舉例

#!/usr/bin/python3# encoding:utf-8’’’Created on 2020-04-19 23:19@author: Administrator’’’import configparserimport osfrom turtle import readconfig#獲取文件絕對路徑 D:commonproDir = os.getcwd()#拼接文件路徑 D:commonconfig.iniconfigPath = os.path.join(proDir, 'config.ini')#創(chuàng)建管理對象config = configparser.ConfigParser()#讀取配置類class readConfig(): #讀取ini文件 config.read(configPath, encoding='UTF-8') #獲取所有的section @staticmethod def get_sections(): return config.sections() @staticmethod def get_items(section): return config.items(section) @staticmethod def get_options(section): return config.options(section) @staticmethod def get_Vaule(section,name): value = config.get(section, name) return value @staticmethod def add_section(): config.add_section(’HTTP’) @staticmethod def set_section(section, option, value): config.set(section, option, value) @staticmethod def remove_seciton(seciton): config.remove_section(seciton) @staticmethod def remove_seciton_value(seciton,key): config.remove_option(seciton,key) if __name__==’__main__’: print(’-----1.打印所有section’) print(readConfig.get_sections()) print(’-----2.打印section=URL的所有key-Value值’) print(readConfig.get_items('URL')) print(’-----3.打印section=URL的所有key值’) print(readConfig.get_options('URL')) print(’-----4.打印section=URL,key=ip的value值’) print(readConfig.get_Vaule(’URL’,’ip’)) print(’-----5.新增之后打印所有section,注意有一個(gè)新增值HTTP’) readConfig.add_section() print(readConfig.get_sections()) print(’-----6.新增section=HTTP,key=port,value=443,查看值,443為新增的值’) readConfig.set_section(’HTTP’, ’port’, ’443’) print(readConfig.get_Vaule(’HTTP’,’port’)) #上面的新增并不會真的真正寫入,需加這個(gè)才能正在寫入ini文件,如果參數(shù)為'w'則表示刪除文件重新寫入,'a'為追加模式寫入 #config.write(open(configPath, 'a')) print(’-----7.刪除sections=URL,打印所有sections,注意URL已被刪除’) readConfig.remove_seciton('URL') print(readConfig.get_sections()) print(’-----8.刪除sections=DATABASE,key=host,打印所有key值,注意host已被刪除’) readConfig.remove_seciton_value(’DATABASE’,’host’) print(readConfig.get_options(’DATABASE’))

運(yùn)行結(jié)果

-----1.打印所有section[’DATABASE’, ’URL’]-----2.打印section=URL的所有key-Value值[(’ip’, ’127.0.0.1’), (’port’, ’8089’)]-----3.打印section=URL的所有key值[’ip’, ’port’]-----4.打印section=URL,key=ip的value值127.0.0.1-----5.新增之后打印所有section,注意有一個(gè)新增值HTTP[’DATABASE’, ’URL’, ’HTTP’]-----6.新增section=HTTP,key=port,value=443,查看值,443為新增的值443-----7.刪除sections=URL,打印所有sections,注意URL已被刪除[’DATABASE’, ’HTTP’]-----8.刪除sections=DATABASE,key=host,打印所有key值,注意host已被刪除[’username’, ’password’, ’port’, ’database’]

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 吕梁市| 托克逊县| 张北县| 桐庐县| 龙陵县| 江城| 华阴市| 东兴市| 瑞金市| 进贤县| 石嘴山市| 石柱| 双鸭山市| 库车县| 肥城市| 恭城| 和林格尔县| 莫力| 获嘉县| 营山县| 阜平县| 栖霞市| 柳江县| 太原市| 利辛县| 建宁县| 深圳市| 新营市| 永济市| 湟中县| 钟山县| 萍乡市| 宁城县| 乌海市| 海晏县| 县级市| 城固县| 鱼台县| 东乡| 宜丰县| 拉萨市|