一、为什么要封装

  我们为什么要封装,我相信你们在项目开发过程中深有体会,那么这个读取配置工具类,又是为了什么?

  为了项目参数配置的灵活性,不要改动到源码

  为了信息的安全(一定层面的),体现代码重用性

  二、封装读取配置文件的操作类

  from configparser import ConfigParser

  # 2.配置文件由哪几部分构成?

  # a、sectionso区域

  # b、option选择项:value值

  class conf_read(object):

  '''封装读取confi/ini等配置文件的操作类'''

  def __init__(self,conf_name,encoding="utf-8):

  # 创建ConfigParser解析对象

  self.conf=ConfigParser()

  self.conf_name=conf_name

  self.encoding=encoding

  # 读取指定配置文件

  self.conf.read(self.conf_name,encoding=self.encoding)

  # 读取数据:查看区域

  self.sections=self.conf.sections()

  self.options=[]

  def get(self,section,option):

  return self.conf.get(section,option)

  def get_int(self,section,option):

  return self.conf.getint(section,option)

  def get_float(self,section,option):

  return self.conf.getfloat(section,option)

  def get_bool(self,section,option):

  return self.conf.getboolean(section,option)

  优化上面封装的读取配置操作类郑州妇科医院哪家好 http://mobile.chfk120.com/

  看上面封装的读取配置操作类,每个方法都只是return简单的一句话源码,其实都只是继承配置类中最原始的方法,所以不用再重复造轮子

  from configparser import ConfigParser

  class conf_read(ConfigParser):

  def __init__(self,conf_name,encoding="utf-8"):

  # 创建ConfigParser解析对象

  super().__init__()

  self.conf_name=conf_name

  self.encoding=encoding

  # 读取指定配置文件

  self.read(self.conf_name,encoding=self.encoding)

  def write_data(self.conf_name)

  '''往配置文件中写入数据'''

  self.set(section, option, value)

  self.write(open(self.file_name,"w",encoding=self.encoding))

  这样看代码,是不是比上一个版本简洁多了,至于读取配置文件的方法,使用继承ConfigParser类的原始方法就好了:

  conf=conf_read("common.conf",encoding="utf-8")

  conf.get("section","option")

Python 之configparser读取配置操作类的更多相关文章

  1. 记一次用python 的ConfigParser读取配置文件编码报错

    记一次用python 的ConfigParser读取配置文件编码报错 ...... raise MissingSectionHeaderError(fpname, lineno, line)Confi ...

  2. Python文件的读取写入操作

    一.打开文件.关闭文件操作 想要读取文件或是写入文件,第一步便是打开文件,最后一步便是关闭文件.这里介绍两种打开(关闭)文件的方式: 1.open()方法 f=open(file_name[,acce ...

  3. python的ConfigParser读取设置配置文件

    python 读写配置文件在实际应用中具有十分强大的功能,在实际的操作中也有相当简捷的操作方案,以下的文章就是对python 读写配置文件的具体方案的介绍,望你浏览完下面的文章会有所收获. pytho ...

  4. Python利用ConfigParser读取配置文件

    http://www.2cto.com/kf/201108/100384.html #!/usr/bin/python # -*- coding:utf-8 -*- import ConfigPars ...

  5. 三十三、python中configparser配置文件相关操作

    配置文件ini [a1]age = 18sex = 'man' [a2]age = 19sex = 'woman'name = False 1.对配置文件进行操作 import configparse ...

  6. python学习1-博客-DB操作类

    #学习python,准备写一个博客,第一天:在别人代码基础上写一个数据库操作的db.py1)python代码 #!/usr/bin/env python # -*- coding: UTF-8 -*- ...

  7. Python项目读取配置的几种方式

    1. 将配置写在Python文件中 配置文件(config.py 或 settings.py) 通常放置在程序源代码的目录,方便引用 配置文件 # settings.py class Config(o ...

  8. Python配置工具类ConfigParser使用

    ConfigParser模块定义了类ConfigParser,用于实现配置文件解释器.该模块ConfigParser在Python3中,已更名为configparser. 一,函数介绍 1.读取配置文 ...

  9. python 提供INI配置文件的操作 ConfigParser

    原文地址:http://www.cnblogs.com/pumaboyd/archive/2008/08/11/1265416.html 红色的为标注信息 +++++++++++++++++引用+++ ...

随机推荐

  1. 添加ssh密钥

    直接运行ssh-keygen,可以不输入密码 $ ssh-keygen Generating public/private rsa key pair. Enter file in which to s ...

  2. 10-numpy笔记-np.random.randint

    b_idx = np.random.randint(0, 9, 90) >>> b_idx array([0, 1, 5, 4, 7, 2, 7, 0, 0, 4, 2, 2, 3, ...

  3. WordCount-JAVA版

    WordCountMapper import java.io.IOException; import org.apache.hadoop.io.IntWritable; import org.apac ...

  4. Apex 中 DML 进阶知识小结

    DML 选项 在 DML 语句执行的时候可以设置选项.这些选项用 DML.Options 类来表示. 完整的介绍在官方文档中. 在建立一个 DML.Options 实例之后,可以使用 setOptio ...

  5. [LeetCode] 829. Consecutive Numbers Sum 连续数字之和

    Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers? ...

  6. [LeetCode] 301. Remove Invalid Parentheses 移除非法括号

    Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...

  7. SpringBoot第十篇:thymeleaf详解

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/10931435.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言   Sprin ...

  8. 遇到 npm WARN npm npm does not support Node.js vx.x.x的解决办法

    遇到如下警告 PS C:\Users\Administrator> npm npm WARN npm npm does not support Node.js v9.11.2 npm WARN ...

  9. 使用eclipse git插件合并merge部分代码方法

    当有一个父项目,它的下面有多个子项目:或者一个项目下边,只想合并部分路径,甚至部分文件的内容,使用下边的方法可以达到目的,特此记录: 1.主项目右键 -> team -> remove f ...

  10. CPU 测评

    PassMark - CPU MarkHigh End CPUs - Updated 22nd of March 2019 Processor CPU Mark Price (USD) Intel C ...