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题目226.翻转二叉树(简单)
题目描述: 翻转一颗二叉树 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 思路分析: 1)递归,不断交换左右子树,直到 ...
- ArcGIS Python 坐标系信息
############################################################# import arcpy import os from arcpy impo ...
- NProgress的使用 及 路由 token 定向的使用
主要配合路由生命周期使用 实现一个进度条的效果 使用方法: 1. 下载:npm install --save nprogress 使用:NProgress.start(); 开始 NProgress ...
- springboot发送邮件,以及携带邮件附件简单使用
可以通过springboot官方文档中Sending Email,找到类似如下java mail的使用文档 https://docs.spring.io/spring/docs/5.1.9.RELEA ...
- 如何将ubuntu16.04升级到ubuntu 18.04?
答:步骤如下: 1. sudo apt update (更新软件源) 2. sudo apt upgrade (更新内核相关的包) 3. sudo apt dist-upgrade 4. sudo ...
- linux系统中的一些典型问题汇总
一.文件系统破坏导致系统无法启动:Checking root filesystem/dev/sda6 contains a file system with errors,check forcedAn ...
- 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_6-1.常用的第三方支付和聚合支付介绍
笔记 第六章 开发在线教育视频站点核心业务之从零基础接入 微信扫一扫网页支付 1.常用的第三方支付和聚合支付介绍 简介:介绍常用的第三方支付和聚合支付 1.什么是第三方支付 第 ...
- WebServer_参考
参考:http://blog.csdn.net/cjsafty/article/details/9323425 这里顺便记录下几个页面 lajphttps://code.google.com ...
- Vim的强大配置文件
我的vim配置主要有以下优点: 1.按F5可以直接编译并执行C.C++.java代码以及执行shell脚本,按“F8”可进行C.C++代码的调试 2.自动插入文件头 ,新建C.C++源文件时自动插入表 ...
- Delphi使用TADOQuery的RowsAffected属性时需要注意的一个点
测试环境是:Delphi 6.Oracle.PLSQL 先创建一个模拟测试的数据表,并且添加几条模拟的数据 create table practice(uno varchar(8), uname va ...