如何使用Pythonini文件-創(chuàng)新互聯(lián)

這篇文章主要為大家展示了如何使用Python ini文件,內(nèi)容簡(jiǎn)而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會(huì)有收獲的,下面讓小編帶大家一起來(lái)看看吧。

創(chuàng)新互聯(lián)建站專注于永德網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供永德?tīng)I(yíng)銷型網(wǎng)站建設(shè),永德網(wǎng)站制作、永德網(wǎng)頁(yè)設(shè)計(jì)、永德網(wǎng)站官網(wǎng)定制、微信平臺(tái)小程序開(kāi)發(fā)服務(wù),打造永德網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供永德網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。

一、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,并以鍵值對(duì)形式輸出(如: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 configparser
import os
from turtle import readconfig

#獲取文件絕對(duì)路徑 D:\common\
proDir = os.getcwd()
#拼接文件路徑 D:\common\config.ini
configPath = os.path.join(proDir, "config.ini")

#創(chuàng)建管理對(duì)象
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'))
  #上面的新增并不會(huì)真的真正寫入,需加這個(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'))

文章標(biāo)題:如何使用Pythonini文件-創(chuàng)新互聯(lián)
網(wǎng)站鏈接:http://muchs.cn/article28/dchpjp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、靜態(tài)網(wǎng)站、網(wǎng)站維護(hù)云服務(wù)器、微信公眾號(hào)域名注冊(cè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司