zoukankan      html  css  js  c++  java
  • python读写增删修改ini配置文件

    一,百度百科

      .ini 文件是Initialization File的缩写,即初始化文件,是windows的系统配置文件所采用的存储格式,统管windows的各项配置,一般用户就用windows提供的各项图形化管理界面就可实现相同的配置了。但在某些情况,还是要直接编辑ini才方便,一般只有很熟悉windows才能去直接编辑。
      开始时用于WIN3X下面,WIN95用注册表代替,以及后面的内容表示一个节,相当于注册表中的键。
     
    二,配置程序

    示例ini配置文件(setting.ini)

     1 [txtA]
     2 name = comma,end,full,run
     3 comma = 1000
     4 end = 3
     5 full = 2
     6 run = 1
     7 default_comma = 3
     8 default_end = 3
     9 default_full = 2
    10 default_run = 1
    11 
    12 [txtB]
    13 name = comma,end,full,run
    14 comma = 1000
    15 end = 3
    16 full = 2
    17 run = 1
    18 default_comma = 3
    19 default_end = 3
    20 default_full = 2
    21 default_run = 1
    22 
    23 [chinese]
    24 name = volume,tones,speed,spokesman
    25 volume = 5
    26 tones = 5
    27 speed = 5
    28 spokesman = 1
    29 default_volume = 5
    30 default_tones = 5
    31 default_speed = 5
    32 default_spokesman = 1
    33 
    34 [english]
    35 name = volume,tones,speed,spokesman
    36 volume = 5
    37 tones = 5
    38 speed = 5
    39 spokesman = 1
    40 default_volume = 5
    41 default_tones = 5
    42 default_speed = 5
    43 default_spokesman = 1
    44 
    45 [help]
    46 
    47 [about]

    示例ini配置文件操作程序1:

    使用configparser函数,缺点:增删、修改都是重写ini文件操作

     1 import configparser
     2 import os
     3 
     4 
     5 # *** 初始化配置文件路径 *** #
     6 curpath = os.path.dirname(os.path.realpath(__file__))   # 当前文件路径
     7 inipath = os.path.join(curpath, "setting.ini")                # 配置文件路径(组合、相对路径)
     8 
     9 # *** 数据读取 *** #
    10 conf = configparser.ConfigParser()
    11 conf.read(inipath, encoding="utf-8")
    12 # sections = conf.sections()              # 获取所有的sections名称
    13 # options = conf.options(sections[0])     # 获取section[0]中所有options的名称
    14 # items = conf.items(sections[0])         # 获取section[0]中所有的键值对
    15 # value = conf.get(sections[-1],'txt2')   # 获取section[-1]中option的值,返回为string类型
    16 # value1 = conf.getint(sections[0],'comma')         # 返回int类型
    17 # value2 = conf.getfloat(sections[0],'end')         # 返回float类型
    18 # value3 = conf.getboolean(sections[0],'run')       # 返回boolen类型
    19 
    20 # *** 删除内容 *** #
    21 # conf.remove_option(sections[0], "comma")
    22 # conf.remove_section(sections[1])
    23 
    24 # *** 修改内容 *** #
    25 conf.set('txtB', "comma", "1000")
    26 conf.write(open(inipath, "r+", encoding="utf-8"))  # r+模式
    27 
    28 
    29 # print(conf.items(sections[0]) )

    示例ini配置文件操作程序2:

    使用configobj 函数

     1 from configobj import ConfigObj
     2 # *** 配置文件预处理 *** #
     3 config = ConfigObj("setting.ini",encoding='UTF8')
     4 
     5 # *** 读配置文件 *** #
     6 # print(config['txtB'])
     7 # print(config['txtB']['name'])
     8 
     9 # *** 修改配置文件 *** #
    10 # config['txtB']['comma'] = "Mufasa"
    11 # config.write()
    12 
    13 # *** 添加section *** #
    14 # config['txtC'] = {}
    15 # config['txtC']['index0'] = "wanyu00"
    16 # config.write()
  • 相关阅读:
    linux部署docker镜像
    SpringBoot 定时任务篇
    POST形式 soapUI调用WebService的restful接口,传入json参数,并且返回json
    Java操作FTP工具类(实例详解)
    MyBatis逆向工程:根据table生成Model、Mapper、Mapper.xml
    Netty完成网络通信(二)
    NIO完成网络通信(一)
    MySQL5.6数据库8小时内无请求自动断开连接
    Eclipse集成Tomcat插件(特别简单)
    程序从sqlserver2008搬家到MySQL5.6
  • 原文地址:https://www.cnblogs.com/Mufasa/p/10845535.html
Copyright © 2011-2022 走看看