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,端口,都会 ...
随机推荐
- go 两个数组取并集
实际生产中,对不同数组取交集.并集.差集等场景很常用,下面来说下两个数组取差集 直接上代码: //两个集合取并集 package main import "fmt" //思想: / ...
- python排序之冒泡排序
def sort(list): for i in range(len(list)): for j in range(len(list) - i - 1): if list[j] < list[j ...
- 20191121-5 Scrum立会报告+燃尽图 02
此作业要求参见https://edu.cnblogs.com/campus/nenu/2019fall/homework/10066 一.小组情况 组长:贺敬文组员:彭思雨 王志文 位军营 徐丽君队名 ...
- ansiblle---roles
使用ansible中的roles来管理主机. 剧本中的roles你现在已经学过 tasks 和 handlers,那怎样组织 playbook 才是最好的方式呢?简单的回答就是:使用 roles ! ...
- mysql数据库索引和引擎
1. 数据库索引 1.1 索引作用 当我们在数据库表中查询数据时,若没有索引,会逐个遍历表格中的所有记录,表格中数据记录量大时很耗时.建立索引就像创建目录一样,直接通过索引找到数据存储位置,加快查找. ...
- nfs服务共享,解决文件没有权限访问问题
最近在了解一些服务权限的设置,突然就被这个nfs服务的权限给绊住了.当你挂载上服务器上的共享目录 时,却无法访问里面的一些内容.内心满满的忧桑...经过努力奋斗几分钟终于搞明白了. 无法访问的原因:因 ...
- 带你体验Android自定义圆形刻度罗盘 仪表盘 实现指针动态改变
带你体验Android自定义圆形刻度罗盘 仪表盘 实现指针动态改变 转 https://blog.csdn.net/qq_30993595/article/details/78915115 近期有 ...
- mybatis之动态SQL操作之更新
1) 更新条件不确定,需要根据情况产生SQL语法,这种情况叫动态SQL /** * 持久层*/ public class StudentDao { /** * 动态SQL--更新 */ public ...
- BrowserUtils
import android.content.Context; import android.content.Intent; import android.net.Uri; public class ...
- 07 MySQL之视图
01-视图的含义 视图是从一个或者多个表中导出的,视图的行为与表非常相似,但视图是一个虚拟表.视图还可以从已经存在的视图的基础上定义. 02-创建视图 # 基本语法格式: CREATE [OR REP ...