日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Oracle使用ini启动,python操作ini类型配置文件的实例教程

發(fā)布時間:2024/1/23 python 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Oracle使用ini启动,python操作ini类型配置文件的实例教程 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、ini文件介紹

INI文件格式是某些平臺或軟件上的配置文件的非正式標準,以節(jié)(section)和鍵(key)構成,常用于微軟Windows操作系統(tǒng)中。這種配置文件的文件擴展名多為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)

# 創(chuàng)建管理對象

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

此封裝實現(xiàn)以下功能:

獲取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標準庫解析實例======根據(jù)需求運行代碼!!!")

# 如果存在mysql.ini先刪除,方便下列代碼的運行

if os.path.exists("mysql.ini"):

os.remove("mysql.ini")

# 我們先寫一些數(shù)據(jù)到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類型配置文件內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

總結

以上是生活随笔為你收集整理的Oracle使用ini启动,python操作ini类型配置文件的实例教程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。