Python3 configparse模块(配置)

2023-11-19

Python3 configparse模块(配置)

参考:https://www.cnblogs.com/bert227/p/9326313.html

         https://www.cnblogs.com/dion-90/p/7978081.html

python2: https://blog.csdn.net/zhouzhiwengang/article/details/72358433

官网地址:https://docs.python.org/3/library/configparser.html

 

 

 

ConfigParser模块在python中是用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section),每个节可以有多个参数(键=值)。 
注意:在python 3 中ConfigParser模块名已更名为configparser

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

config.read('example.ini',encoding="utf-8")

"""读取配置文件,python3可以不加encoding"""

options(section)

"""sections(): 得到所有的section,并以列表的形式返回"""

config.defaults()

"""defaults():返回一个包含实例范围默认值的词典"""

config.add_section(section)

"""添加一个新的section"""

config.has_section(section)

"""判断是否有section"""

print(config.options(section))

"""得到该section的所有option"""

has_option(section, option)

"""判断如果section和option都存在则返回True否则False"""

read_file(f, source=None)

"""读取配置文件内容,f必须是unicode"""

read_string(string, source=’’)

"""从字符串解析配置数据"""

read_dict(dictionary, source=’’)

"""从词典解析配置数据"""

get(section, option, *, raw=False, vars=None[, fallback])

"""得到section中option的值,返回为string类型"""

getint(section,option)

"""得到section中option的值,返回为int类型"""

getfloat(section,option)

"""得到section中option的值,返回为float类型"""

getboolean(section, option)

"""得到section中option的值,返回为boolean类型"""

items(raw=False, vars=None)

"""和items(section, raw=False, vars=None):列出选项的名称和值"""

set(section, option, value)

"""对section中的option进行设置"""

write(fileobject, space_around_delimiters=True)

"""将内容写入配置文件。"""

remove_option(section, option)

"""从指定section移除option"""

remove_section(section)

"""移除section"""

optionxform(option)

"""将输入文件中,或客户端代码传递的option名转化成内部结构使用的形式。默认实现返回option的小写形式;"""

readfp(fp, filename=None)

"""从文件fp中解析数据"""

生成configparser文件实例

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

import configparser  #配置文件

config = configparser.ConfigParser()

"""生成configparser配置文件 ,字典的形式"""

"""第一种写法"""

config["DEFAULT"] = {'ServerAliveInterval': '45',

                     'Compression': 'yes',

                     'CompressionLevel': '9'}

"""第二种写法"""

config['bitbucket.org'] = {}

config['bitbucket.org']['User'] = 'hg'

"""第三种写法"""

config['topsecret.server.com'] = {}

topsecret = config['topsecret.server.com']

topsecret['Host Port'] = '50022'  # mutates the parser

topsecret['ForwardX11'] = 'no'  # same here

 

config['DEFAULT']['ForwardX11'] = 'yes'

"""写入后缀为.ini的文件"""

with open('example.ini', 'w') as configfile:

    config.write(configfile)

运行结果:

1

2

3

4

5

6

7

8

9

10

11

12

[DEFAULT]

serveraliveinterval = 45

compression = yes

compressionlevel = 9

forwardx11 = yes

 

[bitbucket.org]

user = hg

 

[topsecret.server.com]

host port = 50022

forwardx11 = no

读取configparser配置文件的实例

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

import configparser  #配置文件

config = configparser.ConfigParser()

config.read("example.ini")

 

print("所有节点==>",config.sections())

 

print("包含实例范围默认值的词典==>",config.defaults())

 

for item in config["DEFAULT"]:

    print("循环节点topsecret.server.com下所有option==>",item)

 

print("bitbucket.org节点下所有option的key,包括默认option==>",config.options("bitbucket.org"))

 

print("输出元组,包括option的key和value",config.items('bitbucket.org'))

 

print("bitbucket.org下user的值==>",config["bitbucket.org"]["user"]) #方式一

 

topsecret = config['bitbucket.org']

print("bitbucket.org下user的值==>",topsecret["user"]) #方式二

 

print("判断bitbucket.org节点是否存在==>",'bitbucket.org' in config)

 

print("获取bitbucket.org下user的值==>",config.get("bitbucket.org","user"))

 

print("获取option值为数字的:host port=",config.getint("topsecret.server.com","host port"))

运行结果

1

2

3

4

5

6

7

8

9

10

11

12

13

所有节点==> ['bitbucket.org', 'topsecret.server.com']

包含实例范围默认值的词典==> OrderedDict([('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes')])

循环节点topsecret.server.com下所有option==> serveraliveinterval

循环节点topsecret.server.com下所有option==> compression

循环节点topsecret.server.com下所有option==> compressionlevel

循环节点topsecret.server.com下所有option==> forwardx11

bitbucket.org节点下所有option的key,包括默认option==> ['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11']

输出元组,包括option的key和value [('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'hg')]

bitbucket.org下user的值==> hg

bitbucket.org下user的值==> hg

判断bitbucket.org节点是否存在==> True

获取bitbucket.org下user的值==> hg

获取option值为数字的:host port= 50022

删除配置文件section和option的实例(默认分组有参数时无法删除,但可以先删除下面的option,再删分组)

1

2

3

4

5

6

7

8

import configparser  #配置文件

config = configparser.ConfigParser()

config.read("example.ini")

config.remove_section("bitbucket.org")

"""删除分组"""

config.remove_option("topsecret.server.com","host port")

"""删除某组下面的某个值"""

config.write(open('example.ini', "w"))

运行结果

1

2

3

4

5

6

7

8

[DEFAULT]

serveraliveinterval = 45

compression = yes

compressionlevel = 9

forwardx11 = yes

 

[topsecret.server.com]

forwardx11 = no

配置文件的修改实例

1

2

3

4

5

6

7

8

9

"""修改"""

import configparser

config = configparser.ConfigParser()

config.read("example.ini")

config.add_section("new_section")

"""新增分组"""

config.set("DEFAULT","compressionlevel","110")

"""设置DEFAULT分组下compressionlevel的值为110"""

config.write(open('example.ini', "w"))

运行结果

1

2

3

4

5

6

7

8

9

10

[DEFAULT]

serveraliveinterval = 45

compression = yes

compressionlevel = 110

forwardx11 = yes

 

[topsecret.server.com]

forwardx11 = no

 

[new_section]

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python3 configparse模块(配置) 的相关文章

随机推荐

  • js——判断是否是链接格式

    JavaScript无法直接判断一个属性是否是链接 但是可以通过检查属性的值是否符合链接的格式 来初步判断该属性是否是一个链接 通常情况下 链接的URL地址以 http 或 https 开头 因此我们可以用正则表达式来匹配该属性的值是否符合
  • LCD背光调节实验

    目录 LCD 背光调节简介 硬件原理分析 实验程序编写 编译下载验证 编写Makefile 和链接脚本 编译下载 不管是使用显示器还是手机 其屏幕背光都是可以调节的 通过调节背光就可以控制屏幕的亮度 在户外阳光强烈的时候可以通过调高背光来看
  • MATLAB基础学习(二)-变量类型与赋值

    matlab解决问题的最基本思路是建立脚本文件 那么脚本文件的第一段就是定义一些变量 这和C语言等编程思想是一样的 matlab提供的变量类型很多 最基础的是三种 数值变量 符号变量 字符串 其他的类型还有cell table等 这里仅说明
  • Aop做拦截器 获取请求头数据 修改请求数据拦截返回值修改返回值数据

    AOP 拦截器拦截请求头 修改请求参数 请求数据拦截 本页面 按住 ctrl 和 F 搜索 Before doPointcut 返回值数据拦截 本页面 按住 ctrl 和 F 搜索 AfterReturning returning rvt
  • 同一无线络下电脑会打不开个别的网站网页,而手机却可以打开。

    今天打开一个CSDN博客链接 等了好长时间却打不开 后来还发现新浪微博也打不开 昨天还可以 今天就不行了 我的笔记本也没动什么特别的东西 然后我就分析原因 一 首先我的网友可以打开我打不开的这两个链接 相当于打不开链接的代表 说明网站是可以
  • Windows 添加永久静态路由

    route add p 10 10 0 0 mask 255 255 0 0 10 10 6 1 p 参数 p 即 persistent 的意思 p 表示将路由表项永久加入系统注册表
  • 计算机c盘拒绝访问怎么办,怎么解决Win7系统C盘文件拒绝访问

    一位重装系统用户在Win7系统环境下翻开文件 文件夹或者C盘 提示 回绝访问 这是通常由于你的权限不够 假如想要继续访问这些位置就要取代更高的权限 下面就来详细引见一下Win7系统C盘文件回绝访问的处理办法 一 Win7 C盘回绝访问的缘由
  • 【python教程入门学习】Python爬虫入门学习:网络爬虫是什么

    网络爬虫又称网络蜘蛛 网络机器人 它是一种按照一定的规则自动浏览 检索网页信息的程序或者脚本 网络爬虫能够自动请求网页 并将所需要的数据抓取下来 通过对抓取的数据进行处理 从而提取出有价值的信息 认识爬虫 我们所熟悉的一系列搜索引擎都是大型
  • C++模板的使用

    参考博客 https www cnblogs com sevenyuan p 3154346 html 以下内容是摘抄以上博主的博客 1 定义 模板定义 模板就是实现代码重用机制的一种工具 它可以实现类型参数化 即把类型定义为参数 从而实现
  • websphere没有显示服务器,webserver不显示的问题

    运行configurewebserver1 sh时的信息 root iasd10g bin configurewebserver1 sh WASX7209I x 4F7F x 7528 SOAP x 8FDE x 63A5 x 5668 x
  • Python中的any()和all()

    any any 函数采用iterable作为参数 any iterable 迭代器可以是列表 元组或字典 如果iterable中的所有元素为true 则any 函数将返回 True 但是 如果传递给该函数的Iterable为空 则返回 Fa
  • Discord教程:Discord账号注册、Discord多账号登录和管理

    Discord最初是为游戏玩家在群聊和交流而创建的 但自疫情爆发以来 许多企业 公司和初创公司发现 居家办公时使用Discord进行日常沟通非常便捷 Discord不再是仅限于游戏玩家 平台建立了不同于其他任何社交空间的新空间 封闭又开放的
  • 史上最全的《Android面试题及解析》,赶紧收藏!

    写在文章前面的话 工欲行其事 必先利其器 英雄和侠客更需要宝剑助己成功 同样 在现代软件开发环境中 每个Android开发者都需要更好的工具 帮助我们增强功能 提高效率 在这个竞争激烈的行业中 只有优秀的工程师能够生存 需要我们能够为客户提
  • C++-map和set

    本期我们来学习map和set 目录 关联式容器 键值对 pair 树形结构的关联式容器 set multiset map multimap 关联式容器 我们已经接触过 STL 中的部分容器 比如 vector list deque forw
  • layui 勾选不联动父项 树形控件_layui实现checkbox的目录树tree的例子

    废话不多说啦 我就直接上代码吧 需要的朋友可以过来参考下 layui use tree function layui jquery form layui form 获取节点数据 getTreeData function getTreeDat
  • 【华为OD】

    目录 一 题目描述 二 输入描述 三 输出描述 示例一 输入 输出 说明 示例二 输入 输出 说明 四 Java玩法 一 题目描述 现有两个整数数组 需要你找出两个数组中同时出现的整数 并按照如下要求输出 1 有同时出现的整数时 先按照同时
  • stable diffusion基础

    整合包下载 秋叶大佬 AI绘画 8月最新 Stable Diffusion整合包v4 2发布 参照 基础04 目前全网最贴心的Lora基础知识教程 VAE 作用 滤镜 微调 VAE下载地址 C站 https civitai com mode
  • gcc源码编译中的问题处理过程

    1 需求是想要gcc可以编译32位程序也可以编译64位程序 机器是64位的 编译过程教程参考 https www quyu net info 782 html 但是configure配置时不能配置 disable multilib 如果配置
  • Java代码生成器Easy Code

    EasyCode是基于IntelliJ IDEA开发的代码生成插件 支持自定义任意模板 Java html js xml 只要是与数据库相关的代码都可以通过自定义模板来生成 支持数据库类型与java类型映射关系配置 支持同时生成生成多张表的
  • Python3 configparse模块(配置)

    Python3 configparse模块 配置 参考 https www cnblogs com bert227 p 9326313 html https www cnblogs com dion 90 p 7978081 html py