configparser模块

该模块适用于配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键=值)

创建文件

  1. import configparser
  2.  
  3. config = configparser.ConfigParser()
  4.  
  5. config["DEFAULT"] = {'ServerAliveInterval': '',
  6. 'Compression': 'yes',
  7. 'ForwardX11':'yes'
  8. }
  9.  
  10. config['bitbucket.org'] = {'User':'hg'}
  11.  
  12. config['topsecret.server.com'] = {'Host Port':'','ForwardX11':'no'}
  13.  
  14. with open('example.ini', 'w') as configfile:
  15.  
  16. config.write(configfile)

文件内容

  1. [DEFAULT]
  2. serveraliveinterval = 45
  3. compression = yes
  4. forwardx11 = yes
  5.  
  6. [bitbucket.org]
  7. user = hg
  8.  
  9. [topsecret.server.com]
  10. host port = 9999
  11. forwardx11 = no

查找文件内容

方法 描述
defaults() 返回包含实例范围默认值的字典。
sections() 返回可用的section的列表;默认section不包括在列表中
has_section(section) 指示指定的section是否出现在配置中。默认的section未被确认
options(section) 返回指定section中可用的选项列表。
has_option(section, option) 如果给定的section存在,并且包含给定的选项,则返回True;否则返回False
get(section, option) 为指定的section获取一个选项值。
getint(section, option) 它将指定section中的选项强制转换为整数
getfloat(section, option) 它将指定section中的选项强制转换为浮点型
getboolean(section, option) 强制转换为布尔型,”1”, “yes”, “true”, and “on”, 转换为True,”0”, “no”, “false”, and “off”, 转换为Falseo 其他返回ValueError.
items(section) 返回给定section中每个选项的(name,value)对的列表。
  1. import configparser
  2.  
  3. config = configparser.ConfigParser()
  4.  
  5. print(config.sections()) # []
  6. # ---------------------------查找文件内容,基于字典的形式
  7. config.read('example1.ini') # ['bitbucket.org', 'topsecret.server.com']
  8.  
  9. print(config.sections())
  10. print('bytebong.com' in config) # False
  11. print('bitbucket.org' in config) # True
  12.  
  13. print(config['bitbucket.org']["user"]) # hg
  14.  
  15. print(config['DEFAULT']['Compression']) #yes
  16.  
  17. print(config['topsecret.server.com']['ForwardX11']) #no
  18.  
  19. # 其他方式查找文件内容
  20. print(config['bitbucket.org']) #<Section: bitbucket.org>
  21.  
  22. for key in config['bitbucket.org']: # 注意,有default会默认default的键
  23. print(key)
  24.  
  25. print(config.options('bitbucket.org')) # 同for循环,找到'bitbucket.org'下所有键
  26. # ['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11']
  27.  
  28. print(config.items('bitbucket.org')) #找到'bitbucket.org'下所有键值对
  29. # [('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'hg')]
  30.  
  31. print(config.get('bitbucket.org','compression')) # yes get方法Section下的key对应的value
  32. print(config.get('topsecret.server.com','host port')) #
  33.  
  34. for k, v in config.items('bitbucket.org'):
  35. print(k, v)
  36. """
  37. serveraliveinterval 45
  38. compression yes
  39. forwardx11 yes
  40. user hg
  41. """

修改和删除文件内容

增加配置文件中的值

方法 描述
add_section(section) 向实例添加一个section

删除配置文件中的值

方法 描述
remove_option(section, option) 从指定的部分中删除指定的选项。如果该部分不存在,请提出NoSectionError。如果存在的选项被删除,返回True;否则返回False。
remove_section(section) 从配置中删除指定的section。如果这个部分确实存在,返回True。否则返回假

修改配置文件中的值

方法 描述
set(section, option, value) 如果给定的部分存在,将给定的选项设置为指定的值
  1. import configparser
  2.  
  3. config = configparser.ConfigParser()
  4.  
  5. config.read('example.ini')
  6.  
  7. config.add_section('db') # 增加
  8.  
  9. config.remove_section('bitbucket.org') # 删除
  10. config.remove_option('topsecret.server.com',"forwardx11") # 删除
  11.  
  12. config.set('topsecret.server.com','k1','') # 修改
  13. config.set('db','k2','') # 修改
  14.  
  15. config.write(open('new2.ini', "w"))

Python模块——configparser的更多相关文章

  1. 保留注释换行的python模块configparser

    python语言用来解析配置文件的模块是ConfigParser,python3中是configparser模块,我在使用中发现write方法在将配置项重新写入文 件时,配置文件中的空行和注释行都会被 ...

  2. Python模块configparser(操作配置文件ini)

    configparser模块提供对ini文件的增删改查方法. ini文件的数据格式: [name1] attribute1=value1 attribute2=value2 [name2] attri ...

  3. Python模块 - configparser

    configparser模块用来对配置文件进行操作 1.获取所有块 import configparser config = configparser.ConfigParser() config.re ...

  4. python 模块之-configparser

    python 模块configparser   配置文件模块 import configparser    config = configparser.ConfigParser() config[&q ...

  5. python模块(shelve,xml,configparser,hashlib,logging)

    1.1shelve模块 shelve 模块比pickle模块简单,只有一个open函数,返回类似字典对象,可读可写:key必须为字符串, 而值可以是python所支持的数据类型. shelve模块主要 ...

  6. python模块基础之json,requeste,xml,configparser,logging,subprocess,shutil。

    1.json模块 json     用于[字符串]和 [python基本数据类型] 间进行转换(可用于不同语言之前转换),json.loads,将字符串转成python的基本数据类型,json.dum ...

  7. Python之配置文件模块 ConfigParser

    写项目肯定用的到配置文件,这次学习一下python中的配置文件模块 ConfigParser 安装就不说了,pip一下即可,直接来个实例 配置文件 project.conf [db] host = ' ...

  8. python封装configparser模块获取conf.ini值(优化版)

    昨天晚上封装了configparser模块,是根据keyname获取的value.python封装configparser模块获取conf.ini值 我原本是想通过config.ini文件中的sect ...

  9. python中confIgparser模块学习

    python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...

随机推荐

  1. 使用LESS对CSS进行预处理

    LESS 做为 CSS 的一种形式的扩展,它并没有阉割 CSS 的功能,而是在现有的 CSS 语法上,添加了很多额外的功能,所以学习 LESS 是一件轻而易举的事情. 变量 请注意 LESS 中的变量 ...

  2. web项目局部打印

    window.print()方法是打印整个body,若想打印局部区域,网上出现了各种解决办法,我觉得都挺好的.我最推荐jquery.PrintArea.js插件形式 点击上述链接首先下载下来,我的是版 ...

  3. IOS Javascript Date的坑

    Date对象是JavaScript提供的日期和时间的操作接口,它有多种用法.手册上或者网上也有很多文章介绍,这里就不再次复述了. 上次遇到一个坑,这里总结下,也不是什么大问题,若是如果有经验,就不会花 ...

  4. 4412 uboot启动分析

    感谢sea1105, https://blog.csdn.net/sea1105/article/details/52142772 在学习过程中,由于tiny4412资料太过于少,因此参考210的视屏 ...

  5. Docker架构

    Docker使用客户端-服务器(C/S)架构模式,使用远程API来管理和创建Docker容器. Docker容器通过Docker镜像来创建. 容器与镜像的关系类似于面向对象编程中的对象和类. Dock ...

  6. 解决 “access violation at address xxxxxxxxx”错误

    在进行磁盘整理的时候,打开Foxmail的时候出现了“access violation at address32383137”错误 和“access violation at address00000 ...

  7. Codeforces 1082C Multi-Subject Competition 前缀和 A

    Codeforces 1082C Multi-Subject Competition https://vjudge.net/problem/CodeForces-1082C 题目: A multi-s ...

  8. Python12/25--前端之BOM/DOM

    一.DOM 1. 什么是DOM 文档对象模型 Document Object Model 文档对象模型 是表示和操作 HTML和XML文档内容的基础API 文档对象模型,是W3C组织推荐的处理可扩展标 ...

  9. 算法竞赛新编??---WERTYU,UVa10082

    P47 例题:3-2  WERTYU,UVA10082 注:作者的想法是找出输入字符在常量数组中的位置(使用for( i = 1; s[i] && s[i] != c;i++);语句来 ...

  10. java易错题

    (选择二项) 8 A: B: C: D: (选择一项) 9 A: B: C: D: 正确答案是 A 您回答的是 B 回答错误 正确答案是 B,D 您回答的是 A,C 回答错误 (选择一项) 18 A: ...