欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于python的ini配置文件操作工具類

 更新時間:2019年04月24日 09:30:50   作者:CarolLXW  
這篇文章主要為大家詳細(xì)介紹了基于python的ini配置文件操作工具類,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了python的ini配置文件操作工具類的具體代碼,供大家參考,具體內(nèi)容如下

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
  @Time  : 2018/6/22
  @Author : LiuXueWen
  @Site  : 
  @File  : Util_Ini_Operation.py
  @Software: PyCharm
  @Description: ini配置文件操作工具類
    1.讀取.ini配置文件
    2.修改.ini配置文件
    [section]
    option:value
"""
import ConfigParser

'''
  基礎(chǔ)讀取配置文件
    -read(filename)     直接讀取文件內(nèi)容
    -sections()       得到所有的section,并以列表的形式返回
    -options(section)    得到該section的所有option
    -items(section)     得到該section的所有鍵值對
    -get(section,option)  得到section中option的值,返回為string類型
    -getint(section,option) 得到section中option的值,返回為int類型,還有相應(yīng)的getboolean()和getfloat() 函數(shù)。
'''
class get_ini():

  # 初始化配置文件對象
  def __init__(self,path):
    # 實(shí)例化
    self.cf = ConfigParser.ConfigParser()
    # 讀取配置文件
    self.cf.read(path)

  # 獲取所有的sections
  def get_sections(self):
    sections = self.cf.sections()
    return sections

  # 獲取section下的所有key
  def get_options(self,section):
    opts = self.cf.options(section=section)
    return opts

  # 獲取section下的所有鍵值對
  def get_kvs(self,section):
    kvs = self.cf.items(section=section)
    return kvs

  # 根據(jù)section和option獲取指定的value
  def get_key_value(self,section,option):
    opt_val = self.cf.get(section=section,option=option)
    return opt_val

  # 更新指定section的option下的value
  # def update_section_option_val(self,section,option,value,path,module):
  #   self.cf.set(section=section,option=option,value=value)
  #   with open(path,module) as f:
  #     self.cf.write(f)

'''
  基礎(chǔ)寫入配置文件
    -write(fp)             將config對象寫入至某個 .init 格式的文件 Write an .ini-format representation of the configuration state.
    -add_section(section)       添加一個新的section
    -set(section, option, value)    對section中的option進(jìn)行設(shè)置,需要調(diào)用write將內(nèi)容寫入配置文件 ConfigParser2
    -remove_section(section)      刪除某個 section
    -remove_option(section, option)  刪除某個 section 下的 option
'''
class write_ini():

  def __init__(self,path,module):
    # 實(shí)例化配置對象
    self.cf = ConfigParser.ConfigParser()
    # 獲取寫入文件路徑,若采用w+方式則該文件可以不存在
    self.path = path
    # 配置寫入方式,寫入方式"w+"清空寫
    self.module = module

  # 寫入配置文件
  def write_ini_file(self):
    with open(self.path,self.module) as f:
      self.cf.write(f)

  # 新增section
  def add_section(self,section):
    self.cf.add_section(section=section)
    self.write_ini_file()

  # 刪除某個 section
  def remove_section(self,section):
    self.cf.remove_section(section=section)
    self.write_ini_file()

  # 刪除某個 section 下的 option
  def remove_option(self,section,option):
    self.cf.remove_option(section=section,option=option)
    self.write_ini_file()

if __name__ == '__main__':
  pass

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python 解決相對路徑問題:

    Python 解決相對路徑問題:"No such file or directory"

    這篇文章主要介紹了Python 解決相對路徑問題:"No such file or directory"具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • python文件數(shù)據(jù)分析治理提取

    python文件數(shù)據(jù)分析治理提取

    這篇文章主要介紹了python文件數(shù)據(jù)分析治理提取,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-08-08
  • Python迭代和迭代器詳解

    Python迭代和迭代器詳解

    本篇文章主要介紹Python的迭代和迭代器,可迭代對象的相關(guān)概念,有需要的小伙伴可以參考下
    2016-11-11
  • Python3爬蟲RedisDump的安裝步驟

    Python3爬蟲RedisDump的安裝步驟

    在本篇文章里小編給大家整理的是一篇關(guān)于Python3爬蟲RedisDump的安裝步驟,有興趣的朋友們可以學(xué)習(xí)參考下。
    2021-02-02
  • 對Python中的條件判斷、循環(huán)以及循環(huán)的終止方法詳解

    對Python中的條件判斷、循環(huán)以及循環(huán)的終止方法詳解

    今天小編就為大家分享一篇對Python中的條件判斷、循環(huán)以及循環(huán)的終止方法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-02-02
  • Python多線程如何同時處理多個文件

    Python多線程如何同時處理多個文件

    這篇文章主要介紹了Python多線程如何同時處理多個文件問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • python中os操作文件及文件路徑實(shí)例匯總

    python中os操作文件及文件路徑實(shí)例匯總

    這篇文章主要介紹了python中os操作文件及文件路徑,實(shí)例匯總了針對文件的各種常用操作技巧,非常具有實(shí)用價值,需要的朋友可以參考下
    2015-01-01
  • Python解析Laravel?Cookie技巧示例

    Python解析Laravel?Cookie技巧示例

    本文將介紹如何使用Python解析Laravel Cookie,以便在Web開發(fā)中處理這些Cookie數(shù)據(jù),我們將深入了解Cookie的結(jié)構(gòu),以及如何在Python中對其進(jìn)行解析和操作
    2023-12-12
  • python中的list 查找與過濾方法整合

    python中的list 查找與過濾方法整合

    這篇文章主要介紹了python中的list 查找與過濾方法整合,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • python雙向循環(huán)鏈表實(shí)例詳解

    python雙向循環(huán)鏈表實(shí)例詳解

    這篇文章主要為大家詳細(xì)介紹了python雙向循環(huán)鏈表實(shí)例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05

最新評論