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,端口,都会 ...
随机推荐
- 封装带SSH跳板机的MYSQL
一.封装带SSH跳板机的MYSQL 二.配置settting import pymysql from sshtunnel import SSHTunnelForwarder class MyDb(ob ...
- 手把手教你用蒲公英获取udid
如果需要获取udid,但是拥有手机的测试用户身边没有mac电脑和xcode环境, 今天就分享一个快捷的在线获得udid的方法 利用蒲公英网站的获取udid功能 手机浏览器访问 http://www.p ...
- LC 670. Maximum Swap
Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ...
- js对象和jQuery对象相互转换
(1)什么是js对象及代码规则 就是使用js-API,即Node接口中的API或是传统JS语法定义的对象,叫做js对象 js代码规则----divElement var divElement = do ...
- Python写的大小写转换小工具
几行代码的小工具,用于进行如下转换 TRANSACTIONS ON CLOUD COMPUTING => Transactions On Cloud Computing orig = 'TRAN ...
- python 生成词云
1.知识点 """ WordCloud参数讲解: font_path表示用到字体的路径 width和height表示画布的宽和高 prefer_horizontal可以调 ...
- 使用Selenium时解决方案: Exception: Failed to find firefox binary. You can set it by specifying the ······
问题描述: Firefox在自动升级之后,在使用selenium的时候出现了如下错误: Exception: Failed to find firefox binary. You can set it ...
- mgo连接池
package main import ( "log" "sync" "time" "gopkg.in/mgo.v2" ...
- Books Exchange (easy version) CodeForces - 1249B2
The only difference between easy and hard versions is constraints. There are nn kids, each of them i ...
- vs2017安装过程中下载不动的一种情况
第一种可能:微软可能有不同的下载地址,某些地址下载速度快,某些慢.这种情况下,禁用连接,再启用.有几率速度飞速上升. 第二种可能:由于总所周知的原因,连接不了Google.但是如果需要下载Androi ...