歡迎您光臨本站 註冊首頁

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

←手機掃碼閱讀     火星人 @ 2020-04-26 , reply:0

這篇文章主要介紹了Python ini文件常用操作方法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

一、config.ini 配置文件

[DATABASE]

host = 192.1.1.1

username = root

password = root

port = 3306

database = jforum [URL]

#ip,端口

ip =127.0.0.1

port= 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的值(可以理解成,讀取具體某個section下面指定key的值,如config.get('URL','ip')),值:127.0.0.1)

--add_section(section):添加一個section,參數為section的名稱

--set(section, option, value):在section下面添加一條數據(key=value)

--add與set需調用write(open(configPath, "a"))才可以寫入ini文件 #參數a表示最近,w重寫

--remove_seciton(seciton) 刪除整個seciton

--config.remove_option(seciton,key) ,刪除seciton的某個key值


三、源碼舉例

#!/usr/bin/python3 # encoding:utf-8 ''' Created on 2020-04-19 23:19 @author: Administrator ''' import configparser import os from turtle import readconfig #獲取文件絕對路徑 D:common proDir = os.getcwd() #拼接文件路徑 D:commonconfig.ini configPath = os.path.join(proDir, "config.ini") #創建管理對象 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,注意有一個新增值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')) #上面的新增並不會真的真正寫入,需加這個才能正在寫入ini文件,如果參數為"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'))

運行結果

-----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,注意有一個新增值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']


[火星人 ] Python ini文件常用操作方法解析已經有377次圍觀

http://coctec.com/docs/python/shhow-post-231723.html