欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

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

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

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

一、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,參數(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) 刪除整個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:\common\config.ini
configPath = 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,注意有一個新增值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文件,如果參數(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'))

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

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

廣告

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

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