一,百度百科

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

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

 [txtA]
name = comma,end,full,run
comma = 1000
end = 3
full = 2
run = 1
default_comma = 3
default_end = 3
default_full = 2
default_run = 1 [txtB]
name = comma,end,full,run
comma = 1000
end = 3
full = 2
run = 1
default_comma = 3
default_end = 3
default_full = 2
default_run = 1 [chinese]
name = volume,tones,speed,spokesman
volume = 5
tones = 5
speed = 5
spokesman = 1
default_volume = 5
default_tones = 5
default_speed = 5
default_spokesman = 1 [english]
name = volume,tones,speed,spokesman
volume = 5
tones = 5
speed = 5
spokesman = 1
default_volume = 5
default_tones = 5
default_speed = 5
default_spokesman = 1 [help] [about]

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

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

 import configparser
import os # *** 初始化配置文件路径 *** #
curpath = os.path.dirname(os.path.realpath(__file__)) # 当前文件路径
inipath = os.path.join(curpath, "setting.ini") # 配置文件路径(组合、相对路径) # *** 数据读取 *** #
conf = configparser.ConfigParser()
conf.read(inipath, encoding="utf-8")
# sections = conf.sections() # 获取所有的sections名称
# options = conf.options(sections[0]) # 获取section[0]中所有options的名称
# items = conf.items(sections[0]) # 获取section[0]中所有的键值对
# value = conf.get(sections[-1],'txt2') # 获取section[-1]中option的值,返回为string类型
# value1 = conf.getint(sections[0],'comma') # 返回int类型
# value2 = conf.getfloat(sections[0],'end') # 返回float类型
# value3 = conf.getboolean(sections[0],'run') # 返回boolen类型 # *** 删除内容 *** #
# conf.remove_option(sections[0], "comma")
# conf.remove_section(sections[1]) # *** 修改内容 *** #
conf.set('txtB', "comma", "")
conf.write(open(inipath, "r+", encoding="utf-8")) # r+模式 # print(conf.items(sections[0]) )

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

使用configobj 函数

 from configobj import ConfigObj
# *** 配置文件预处理 *** #
config = ConfigObj("setting.ini",encoding='UTF8') # *** 读配置文件 *** #
# print(config['txtB'])
# print(config['txtB']['name']) # *** 修改配置文件 *** #
# config['txtB']['comma'] = "Mufasa"
# config.write() # *** 添加section *** #
# config['txtC'] = {}
# config['txtC']['index0'] = "wanyu00"
# config.write()

最新文章

  1. oracle的round函数和trunc函数
  2. Android笔记——探究活动
  3. ASP.NET MVC学习之模型验证篇
  4. http请求和http响应详细解析
  5. XML小总结
  6. 用CSS让字体在一行内显示不换行(收藏)
  7. chrome跨域配置
  8. UVa 10670 - Work Reduction
  9. MySQL索引统计信息更新相关的参数
  10. Linux技巧:一次删除一百万个文件的最快方法
  11. python+redis+kairosdb+cassandra+mysql环境搭建
  12. 支持动态调频_配置AXP228电源管理_4核8核兼容设计_iTOP-4418/6818开发板
  13. 深入理解Redis内存模型
  14. python---自己来打通节点,链表,栈,应用
  15. 负载均衡---ribbon
  16. centos单机安装nginx、gitlab、nexus、mysql共存
  17. 洛谷 P4128: bzoj 1815: [SHOI2006]有色图
  18. 大型Web 网站 Asp.net Session过期你怎么办
  19. nodejs基础 -- web模块
  20. BootStrap的栅格系统的基本写法(布局)

热门文章

  1. 本地访问weblogic控制台无反应,关闭linux操作系统防火墙
  2. Ubuntu18.04 server安装步骤
  3. SpringMVC和AJAX交互
  4. ubuntu 17.04 添加用户到sudo组
  5. springboot使用RestTemplate以post方式发送json字符串参数(以向钉钉机器人发送消息为例)
  6. vs2017中信号与槽连接
  7. Android下文件访问的权限
  8. linux 基础学习常见问题
  9. Arrays.asList()使用指南
  10. delphi循环校验数据集