Oracle使用ini启动,python操作ini类型配置文件的实例教程
一、ini文件介紹
INI文件格式是某些平臺或軟件上的配置文件的非正式標準,以節(section)和鍵(key)構成,常用于微軟Windows操作系統中。這種配置文件的文件擴展名多為INI
二、ini文件的結構
片段[section]
鍵名 option
值 value
三、實例:
實例1
python25.ini
[teachers]
name = ['yushen', 'pianpian']
age = 16
gender = '女'
favor = {"movie": "追風", "music": "周杰倫"}
[student]
name = ['啦啦迷弟', '啦啦迷妹']
age = 18
操作ini文件
from configparser import ConfigParser
# 初始化
config = ConfigParser()
# 讀取文件
config.read('python25.ini', encoding='utf-8')
a = config.get('teachers', 'name')
print(a)
print(type(a))
運行結果如下:
實例2
fz.ini
讀取fz.ini文件代碼:
import configparser
import os
curpath = os.path.dirname(os.path.realpath(__file__))
cfgpath = os.path.join(curpath, "fz.ini")
# fz.ini的路徑
print(cfgpath)
# 創建管理對象
conf = configparser.ConfigParser()
# 讀ini文件
conf.read(cfgpath, encoding="utf-8")
# 獲取所有的section
sections = conf.sections()
# 返回list
print(sections)
items = conf.items('oracle')
# list里面對象是元祖
print(items)
運行結果:
實例3,封裝升級
set修改,add添加,write寫入、remove刪除
此封裝實現以下功能:
獲取sections列表
獲取指定的section的options列表
獲取指定section的配置信息列表
按類型讀取配置信息
新增section
設置指定option值
刪除指定section
刪除指定option
# -*- coding:utf-8 -*-
from configparser import ConfigParser
import os
class TEINI:
def __init__(self, path):
self.path = path
self.ini = ConfigParser()
self.ini.read(self.path)
# 獲取sections列表
def get_sections(self):
if self.ini:
return self.ini.sections()
# 獲取指定的section的options列表
def get_options_by_section(self, section):
if self.ini:
return self.ini.options(section)
# 獲取指定section的配置信息列表
def get_section_items(self, section):
if self.ini:
return self.ini.items(section)
# 按類型讀取配置信息
# 返回字符串類型
def get_string(self, section, option):
if self.ini:
return self.ini.get(section, option)
# 返回int類型
def get_int(self, section, option):
if self.ini:
return self.ini.getint(section, option)
# 返回float類型
def get_float(self, section, option):
if self.ini:
return self.ini.getfloat(section, option)
# 返回bool類型
def get_boolean(self, section, option):
if self.ini:
return self.ini.getboolean(section, option)
# 新增section
def add_section(self, section):
if self.ini:
self.ini.add_section(section)
self.ini.write(open(self.path, "w"))
# 設置指定option值
def set_option(self, section, option, value):
if self.ini:
self.ini.set(section, option, value)
self.ini.write(open(self.path, "w"))
# 刪除指定section
def remove_section(self, section):
if self.ini:
self.ini.remove_section(section)
self.ini.write(open(self.path, "w"))
# 刪除指定option
def remove_option(self, section, option):
if self.ini:
self.ini.remove_option(section, option)
self.ini.write(open(self.path, "w"))
if __name__ == "__main__":
print("python ini標準庫解析實例======根據需求運行代碼!!!")
# 如果存在mysql.ini先刪除,方便下列代碼的運行
if os.path.exists("mysql.ini"):
os.remove("mysql.ini")
# 我們先寫一些數據到mysql.ini中
ini = TEINI("mysql.ini")
# 先加一個mysql section
mysql_section = "mysql"
ini.add_section(mysql_section)
# 在mysql section下寫入一些配置信息
ini.set_option(mysql_section, "host", "192.168.3.1")
ini.set_option(mysql_section, "port", "3306")
ini.set_option(mysql_section, "db", "mysql")
ini.set_option(mysql_section, "user", "admin")
ini.set_option(mysql_section, "password", "111111")
# 再添加一個oracle section
oracle_section = "oracle"
ini.add_section(oracle_section)
# oracle section下寫入一些配置信息
ini.set_option(oracle_section, "host", "192.172.0.1")
ini.set_option(oracle_section, "port", "8080")
ini.set_option(oracle_section, "db", "oracle")
ini.set_option(oracle_section, "user", "guiyin")
ini.set_option(oracle_section, "password", "666666")
# 獲取下所有的section,并在console輸出
sections = ini.get_sections()
print(sections)
# 遍歷各個section下的options,并在console中輸出
print("===" * 20)
for sec in sections:
print(sec, " 中的options為: ")
options = ini.get_options_by_section(sec)
print(options)
print("===" * 20)
# 獲取各個section下的配置信息
for sec in sections:
print(sec, " 中的配置信息為: ")
items = ini.get_section_items(sec)
print(items)
print("***" * 20)
# 獲取指定的option值這里演示讀取host和port
host = ini.get_string("mysql", "host")
port = ini.get_int("mysql", "port")
print("類型: ", type(host), " ", type(port))
print(host, " ", port)
# 刪除mysql中的host配置
ini.remove_option("mysql", "host")
# 刪除oracle section
ini.remove_section("oracle")
# 修改mysql port的值為4000
ini.set_option("mysql", "port", "5538")
# 最終mysql.ini中的文件內容如下
# [mysql]
# port = 5538
# db = mysql
# user = admin
# password = 111111
items = ini.get_section_items("mysql")
print(items)
print("!!!" * 20)
運行結果如下:
總結
到此這篇關于python操作ini類型配置文件的文章就介紹到這了,更多相關python操作ini類型配置文件內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!
總結
以上是生活随笔為你收集整理的Oracle使用ini启动,python操作ini类型配置文件的实例教程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 分区表需要数据备份吗oracle,Ora
- 下一篇: php soap 用户名密码,PHP S