python读写增删修改ini配置文件
一,百度百科
示例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()
python读写增删修改ini配置文件的更多相关文章
- python开发_configparser_解析.ini配置文件工具_完整版_博主推荐
# # 最近出了一趟差,是从20号去的,今天回来... # 就把最近学习的python内容给大家分享一下... # ''' 在python中,configparser模块提供了操作*.ini配置文件的 ...
- python读取uti-8格式ini配置文件出现UnicodeDecodeError: 'gbk' codec can't decode byte 0xba in position 367: illegal multibyte sequence错误解决方法
出现这种错误只需要在read下添加encoding='utf-8' 如: from configparser import ConfigParser cf = ConfigParser() cf.re ...
- asp.net 操作INI文件的读写,读写操作本地ini配置文件
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu ...
- python作业之修改用户配置文件
用户的配置文件如下 backend oldboy school school1 age 21 weight 210 qq 550176565 iphone 139987676backend oldgi ...
- 用java读写ini配置文件
本文转载地址: http://www.blogjava.net/silvernapoleon/archive/2006/08/07/62222.html import java.io.Bu ...
- java 读写ini配置文件
ini配置文件 ;客户端配置[Client];客户端版本号version=0001;设备号devNum=6405 public final class ConfigurationFile { /** ...
- python读写修改配置文件(ini)
python 有时候参数需要保存到配置文件中 接下来总结一下 配置文件的读写和修改的操作 代码如下: #!/usr/bin/env python # -*- coding: utf- -*- # 读 ...
- python 提供INI配置文件的操作 ConfigParser
原文地址:http://www.cnblogs.com/pumaboyd/archive/2008/08/11/1265416.html 红色的为标注信息 +++++++++++++++++引用+++ ...
- Python中操作ini配置文件
这篇博客我主要想总结一下python中的ini文件的使用,最近在写python操作mysql数据库,那么作为测试人员测试的环境包括(测试环境,UAT环境,生产环境)每次需要连接数据库的ip,端口,都会 ...
随机推荐
- Leetcode题目206.反转链表(简单)
题目描述: 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL 进阶: ...
- 20175329&20175313&20175318 2019-2020 《信息安全系统设计基础》实验一
详见 https://www.cnblogs.com/xiannvyeye/p/11792152.html#%E4%B8%80%E5%AE%9E%E9%AA%8C%E5%86%85%E5%AE%B9
- 【软件工程】Beta冲刺(5/5)
链接部分 队名:女生都队 组长博客: 博客链接 作业博客:博客链接 小组内容 恩泽(组长) 过去两天完成了哪些任务 描述 将数据分析以可视化形式展示出来 新增数据分析展示等功能API 服务器后端部署, ...
- [转]Nginx实现高并发的原理
Nginx 首先要明白,Nginx 采用的是多进程(单线程) & 多路IO复用模型.使用了 I/O 多路复用技术的 Nginx,就成了”并发事件驱动“的服务器. 异步非阻塞(AIO)的详解ht ...
- VBA锁定指定单元格
Then .Range("AF4").Value = pjno .Range("A1:AH56").Locked = False .Range("F6 ...
- 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-6.Mysql逆向工程效率神器之使用IDE自动生成Java实体类
笔记 6.Mysql逆向工程效率神器之使用IDE自动生成Java实体类 简介:实战使用IDE根据Mysql自动生成java pojo实体类 1.IDEA连接数 ...
- 解决Ubuntu MySQL服务无法远程登录
一.10061错误 Ubuntu安装MySQL . sudo apt-get install mysql-server . apt-get isntall mysql-client . sudo ap ...
- linux简单命令---网络编程
---------------------------------------------------------------------------------------------------- ...
- Error response from daemon: driver failed programming external connectivity on endpoint httptest (9bb351e8d0738501ae2c57d1bfe3b18aced708d9dc66a63f642c5918cb144340): (iptables failed: iptables --wait
来自守护程序的错误响应:驱动程序未能在终结点httptest上对外部连接进行编程 解决方法: [root@localhost ~]# pkill docker [root@localhost ~]# ...
- Java 泛型,你了解类型擦除吗?
泛型,一个孤独的守门者. 大家可能会有疑问,我为什么叫做泛型是一个守门者.这其实是我个人的看法而已,我的意思是说泛型没有其看起来那么深不可测,它并不神秘与神奇.泛型是 Java 中一个很小巧的概念,但 ...