python向config、ini文件读取写入
config读取操作
cf = configparser.ConfigParser() # 实例化对象
cf.read(filename) # 读取文件
cf.sections() # 读取sections值, 返回一个list
cf.options(sections) # 读取options值, 返回一个list
cf.items(sections) # 读取指定sections下所有的键值对, 返回list
cf.get(sections, key) # 获取指定sections下指定key的value值, 返回str
cf.getint(sections, key) # 获取指定sections下指定key的value值, 返回int
# encoding: utf-8 import configparser
import os class ReadConifg():
"""读取配置文件"""
def __init__(self, filename, filepath):
os.chdir(filepath) # 将当前工作目录切换到指定的目录
self.cf = configparser.ConfigParser() # 实例化configparser对象
self.cf.read(filename) # 读取文件 def read_sections(self):
"""读取配置文件中sections"""
sacs = self.cf.sections() # 获取sections,返回list
print("sections:", sacs, type(sacs)) def read_options(self, sections):
"""获取配置文件中的options值"""
opts = self.cf.options(sections) # 获取db section下的options,返回list
print("%s:%s" % (sections, opts), type(opts)) def read_kv(self, sections):
"""获取配置文件中的所有键值对"""
kvs = self.cf.items(sections) # 获取db section下的所有键值对,返回list
print("%s:%s" % (sections, kvs)) def get_str(self, sections, key):
"""获取指定sectons中指定的key的value值, 返回的会是 str 类型"""
value_str = self.cf.get(sections, key)
return value_str def get_int(self, sections, key):
"""获取指定sectons中指定的key的value值, 返回的会是 int 类型"""
value_int = self.cf.getint(sections, key)
return value_int
读取config文件
config写入操作
cf = configparser.ConfigParser() # 实例化对象
cf.read(self.filename) # 读取文件,如果是重新写入,覆盖原有内容不需要读取
cf.add_section(section) # 添加sections值
cf.set(section, option, value) # 在指定的sections中添加键值对
cf.remove_section(section) # 移除sections, 需要先cf.read(filename)
cf.remove_option(section, option) # 移除指定sections下的options, 需要先cf.read(filename)
class WriteConfig():
"""写入config文件"""
def __init__(self, filename, filepath=r"D:\python_file\boke\config"):
self.filename = filename
os.chdir(filepath)
self.cf = configparser.ConfigParser()
self.cf.read(self.filename) # 如果修改,则必须读原文件 def _with_file(self):
# write to file
with open(self.filename, "w+") as f:
self.cf.write(f) def add_section(self, section):
# 写入section值
self.cf.add_section(section)
self._with_file() def set_options(self,section, option, value=None):
"""写入option值"""
self.cf.set(section, option, value)
self._with_file() def remove_section(self, section):
"""移除section值"""
self.cf.remove_section(section)
self._with_file() def remove_option(self, section, option):
"""移除option值"""
self.cf.remove_option(section, option)
self._with_file()
写入config文件
python向config、ini文件读取写入的更多相关文章
- INI文件的写入与读取
INI文件的写入与读取 [节名] '[]中的节名对应此API的第一参数 Name=内容 'Nmae对应此API的第二参数 API的第三参数是没有取到匹配内容时返回的字符串; ...
- 轮子:读取config.ini文件
python: 把config.ini文件成map返回 def get_conf(conf_file): conf = {} ll=list(map(lambda x: x.replace('&quo ...
- C#对config.ini文件进行读取和修改
C#对config.ini文件进行读取和修改: public partial class Patrolcar : Form之后可以加入如下类: #region public class IniFile ...
- PostgreSql那点事(文件读取写入、命令执行的办法)
• 2013/07/9 作者: admin PostgreSql那点事(文件读取写入.命令执行的办法) 今天无意发现了个PostgreSQL环境,线上学习了下,一般的数据注射(读写数据库)差异不大,不 ...
- 1. 在config.ini文件中加入dm.park.time=1,会使uap中的tomcat启动加快
在config.ini文件中加入dm.park.time=1,会使uap中的tomcat启动加快
- python中readline判断文件读取结束的方法
注:内容来自网络 本文实例讲述了python中readline判断文件读取结束的方法.分享给大家供大家参考.具体分析如下: 大家知道,python中按行读取文件可以使用readline函数,下面现介绍 ...
- 91.生成ini文件并写入和读取ini文件
写入 WritePrivateProfileStringA("hello money", infx[i].name, money, "1.ini"); 按照字符 ...
- C# 创建INI文件,写入并可读取。----转载
基于C#winform设计. 首先创建一个类,我命名为IniFiles.并引入命名空间using System.Runtime.InteropServices; 接着,声明API函数 [DllImpo ...
- 【代码】ini 文件读取工具类
using System; using System.Runtime.InteropServices; using System.Text; namespace hrattendance.Common ...
随机推荐
- vue.js 精学组件记录
组件需要注册后才可以使用. Vue.component('my-component',{ template:'<div>这是组件内容</div>' }): 局部注册组件 var ...
- ArcGIS中的坐标系:基本概念和常用操作(二)
关于坐标系,上次的内容还没有结束. 上次已经解释清楚关于投影坐标系和地理坐标系的区别了,它们在实际应用上也有诸多不同. 下面这张图应该比较清晰 我们经常能发现,当计算面积时,提示面积已禁用,原因就在于 ...
- Python:从入门到实践--第九章-类--练习
#.餐馆:创建一个名为Restaurant的类,其方法_init_()设置两个属性:restaurant_name和cuisine_type. #创建一个名为describe_restaurant的方 ...
- deemo
第一部分:结缘计算机 你为什么选择计算机专业?你认为你的条件如何?和这些博主比呢? 因为亲戚的推荐,就业前景好.个人条件的话,算一般.与其他博主相比还是有些差距的. 第二部分:在计算机系里学习 你对你 ...
- Spock - Document - 03 - Data Driven Testing
Data Driven Testing Peter Niederwieser, The Spock Framework TeamVersion 1.1 Oftentimes, it is useful ...
- 2018-2019-2 20165313《网络对抗技术》Exp1 缓冲区溢出实验
实践涉及指令 NOP:NOP指令即"空指令".执行到NOP指令时,CPU什么也不做,仅仅当做一个指令执行过去并继续执行NOP后面的一条指令.(机器码:90) JNE:条件转移指令, ...
- PyCharm设置仿sublime配色__Py版本2018.3.5
效果图: 导出settings 模板为: 1.https://files.cnblogs.com/files/xier/PyCharm_settings.zip 2.https://files.cnb ...
- 网络编程一定要看过的socket大山
python已经可以做很多的东西了.但是要想要和别人互联互通就会涉及到一个关键的模块socket!值得一提的是,其实socket不是python独创的一种模块,而是任何语言都会有的一个部分!自己的程序 ...
- 数据看板superset在Windows环境下安装
@https://www.cnblogs.com/calmzeal/archive/2017/08/14/7359144.html 以下是我的安装版本与安装步骤: 1. 安装Python 3.7 , ...
- cifX使用心得
一.注意事项: 1.xChannelIORead()第一次读取到数据是空的.2.系统安装cifX驱动之前一定先安装有uio模块和libpciaccess库:建议系统内核为3.X.X版本,测试版本为3. ...