config  parser

  用于解析配置文件的模拟

  何为配置文件

  包含配置程序信息的文件就是称为配置文件

  什么样的数据应作为配置文件

  需要该  但是不经常改的信息  例如数据文件的路径  db_path

  配置文件中  只用两种内容

  一种section  分区

  一种option    选项  就是一个key = value  形式

我们用的最多的就是get功能 用来从配置文件获取一个配置选项

import configparser
# 创建一个解析器
config = configparser.ConfigParser()
# 读取并解析test.cfg
config.read("test.cfg",encoding="utf-8")
# 获取需要的信息
# 获取所有分区
# print(config.sections())
# 获取所有选项
# print(config.options("user"))
# 获取某个选项的值
# print(config.get("path","DB_PATH"))
# print(type(config.get("user","age")))
#
# # get返回的都是字符串类型 如果需要转换类型 直接使用get+对应的类型(bool int float)
# print(type(config.getint("user","age")))
# print(type(config.get("user","age"))) # 是否由某个选项
config.has_option()
# 是否由某个分区
# config.has_section() # 不太常用的
# 添加
# config.add_section("server")
# config.set("server","url","192.168.1.2")
# 删除
# config.remove_option("user","age")
# 修改
# config.set("server","url","192.168.1.2") # 写回文件中
# with open("test.cfg", "wt", encoding="utf-8") as f:
# config.write(f)

configparse 模块的更多相关文章

  1. python configparse模块&xml模块

    configparse模块 用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. [DEFAULT] serveraliveinterval = ...

  2. 【转】Python3 configparse模块(配置)

    [转]Python3 configparse模块(配置) ConfigParser模块在python中是用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(s ...

  3. python模块: hashlib模块, configparse模块, logging模块,collections模块

    一. hashlib模块 Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定的数据串(通常用 ...

  4. 14 ConfigParse模块

    1.ConfigParse模块的基本概念 此模块用于生成和修改常见配置文档. ConfigParser 是用来读取配置文件的包. 配置文件的格式如下:中括号“[ ]”内包含的为section.sect ...

  5. configParse模块

    一.配置文件简介 在各种程序里面都有配置文件,为了对配置文件进行操作. python中引入了configParse模块进行操作. 配置数值类型: 配置文件中,我们看到的bool型,整数型,在我们操作的 ...

  6. 0423 hashlib模块、logging模块、configparse模块、collections模块

    一.hashlib模块补充 1,密文验证 import hashlib #引入模块 m =hashlib.md5() # 创建了一个md5算法的对象 m.update(b') print(m.hexd ...

  7. Python模块-configparse模块

    configparse模块用来解析配置文件 配置文件 [DEFAULT] port = 3306 socket = /tmp/mysql.sock [mysqldump] max_allowed_pa ...

  8. Python进阶-XVV hashlib模块、configparse模块、logging模块

    1.配置相关的configparse模块 配置文件如何组织?python中常见的是将配置文件写成py,然后引入该模块即可.优点是方便访问. 但是也有用类似windows中的ini文件的配置文件,了解即 ...

  9. python学习-58 configparse模块

    configparse模块 1.生成文件 import configparser # 配置解析模块 config = configparser.ConfigParser() # config = { ...

  10. python基础--xml和configparse模块

    1)XML模块 xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多. 下面是xml的遍历查询删除修改和生成 # -*- coding:utf-8 -*- __author__ = 's ...

随机推荐

  1. 自动化测试基础-断言(Assert)使用方法

    junit中的assert方法全部放在Assert类中,总结一下junit类中assert方法的分类.1.assertTrue/False([String message,]boolean condi ...

  2. leecode第二十六题(删除排序数组中的重复项)

    class Solution { public: int removeDuplicates(vector<int>& nums) { int len=nums.size(); ) ...

  3. es6 - 函数 扩展

    1. 可添加默认参数 function fn(name,age=17){ console.log(name+","+age); } fn("Amy",18); ...

  4. Codeforces Gym - 101102B - The Little Match Girl

    B. The Little Match Girl time limit per test 1 second memory limit per test 256 megabytes input stan ...

  5. 第 8 章 容器网络 - 052 - overlay 如何实现跨主机通信?

    overlay 如何实现跨主机通信? 在 host2 中运行容器 bbox2: docker run -itd --name bbox2 --network ov_net1 busybox bbox2 ...

  6. VMware vSphere client 中英文语言界面设置

    安装的时候可以选择简体中文,然后安装后,对vsphere client的执行程序制作快捷方式 以win7 x64环境默认路径为例 英文启动如下: "C:Program Files (x86) ...

  7. Python 编程快速上手 第五章总结

    第五章 字典和结构化数据 创建数组 格式:myCat = {'size':'fat','color':'gray',disposition':'loud'} 对字典的操作 通过[ ] 访问字典的值 [ ...

  8. GenomicConsensus (quiver, arrow)使用方法 | 序列 consensus

    https://github.com/PacificBiosciences/GenomicConsensus GenomicConsensus 是pacbio开发的,我个人非常不喜欢pacbio开发的 ...

  9. MyEclipse下自定义(支持html5的)JSP模板--JSP

    需求:由于某些Mclipse版本发行的比较早,所以在有些版本(比如Mclipse2014,至于其他版本博主不知能不能创建html5格式的JSP页面)里创建JSP页面时html部分不是html5格式的. ...

  10. android -------- 蓝牙Bluetooth

    什么是蓝牙? 也可以说是蓝牙技术.所谓蓝牙(Bluetooth)技术,实际上是一种短距离无线电技术,是由爱立信公司公司发明的.利用“蓝牙”技术,能够有效地简化掌上电脑.笔记本电脑和移动电话手机等移动通 ...