Python3的configparser模块的使用

news/2024/7/8 12:19:50
import configparser

config = configparser.ConfigParser()

# 字典模式生成配置文件
# 第一个section
config['DEFAULT'] = {
    'A': 'abc',
    'B': '123',  # 数字也要写成string类型
    'C': 'hello'}

# 第二个section
config['Head'] = {'H1': '100', 'H2': '200', 'H3': '300'}

# 第三个section
config['www'] = {'W1': '199', 'W2': '299'}

# 生成配置文件 并写入
with open('example.ini', 'w') as configfile:
    config.write(configfile)

# 读取配置文件
config.read('example.ini', encoding='utf8')

# 打印section时,不会打印默认DEFAULTS,以list形式打印
print('config.sections():', config.sections())

# 打印defaults 时 会以键值对,元组形式打印
print('config.defaults():', config.defaults())

# 判断section是否存在
print('www' in config)  # 存在 True
print('mmm' in config)  # 不存在 False

# 添加 section
config.add_section('T')
print(config.sections())

# 打印[指定section][指定键名]
print(config['Head']['H2'])

# 打印某一个非default section时,会将默认内容一并打印
for key in config['Head']:
    print(key)

# set() 修改 键值 指定section,指定键名,修改成的值
config.set('www', 'W2', '2000')

# 删除section
config.remove_section('T')
print(config.sections())

# 删除键 option
config.remove_option('www', 'W1')  # 注意 如果后续没有重新写入文件操作,该操作不会真正成功

# 只能新建 或是 覆盖原文件  不能直接修改
config.remove_section('www')  # 注意 如果后续没有重新写入文件操作,该操作不会真正成功

# 重新写入操作,也叫持久化操作 可以把下面注释掉看看配置文件 上面的删除操作不会成功
with open('example.ini', 'w') as configfile:
    config.write(configfile)

要点:

  •  config['section']={dict}  ,dict字典赋值
  • 生成配置文件,类似文件操作。with open('filename','w') as configfile: config.write(configfile)
  • 利用config.section() 获取section时,以list形式打印,不会打印默认DEFAULTS
  • 利用config.defaults() 获取defaults时,会以键值对,元组形式打印
  • 判断section是否存在:'section' in config
  • 添加section:config.add_section('section_name')
  • 打印某section下的键的值:config['section_name']['key']
  • 修改某section下的键的值:config.set('section_name','key','value')
  • 删除某section:config.remove_section('section_name')
  • 删除某section下的某个键:confg.remove_option('section_name','key')
  • 持久化操作,就是文件的写入,在完成配置文件的增删改后,重新写入。with open('filename','w') as configfile: config.write(configfile)

 


http://www.niftyadmin.cn/n/4827484.html

相关文章

Hadoop生态圈介绍及入门(转)

本帖最后由 howtodown 于 2015-4-2 23:15 编辑 问题导读1.Hadoop生态圈介绍了哪些组件,分别都是什么?2.大数据与Hadoop是什么关系?本章主要内容:理解大数据的挑战了解Hadoop生态圈了解Hadoop发行版使用基于Hadoop的企业级应用你可…

Python爬虫:常用浏览器的useragent

1,为什么需要修改UserAgent在写python网络爬虫程序的时候,经常需要修改UserAgent,有很多原因,罗列几个如下:不同Agent下看到的内容不一样,比如,京东网站上的手机版网页和pc版网页上的商品优惠不…

JS 事件 0401

1.一些标签的禁用属性 标签的一些属性 readonly 只读属性 禁止使用标签,不能操作,但是可以传参 只能看,不能改,可以传参disabled 禁用属性 禁止使用标签,不能操作,也不能传参 你就当没有这个标签multiple 多选属性 file,select,可以按ctrl进行多选 都是 布尔属性 在JavaScri…

Python3的hashlib模块的使用

import hashlibm1 hashlib.md5() m2 hashlib.sha1() m3 hashlib.sha3_256() m4 hashlib.sha512() # 不加盐 m_4 hashlib.sha512(R.encode(utf-8)) # 加盐m1.update(今天晚上去哪吃饭.encode(utf8)) # 默认为Unicode 需要的是bit 所以需要转换 print(A:今天晚…

所有事件event集锦

mousedown touchstart, mousemove touchmove, mouseup mouseleave touchend touchleave touchcancel, wheel mousewheel DOMMouseScroll, ondragstart 事件 dragmove事件 dragend事件 ondrop 事件 在拖动目标上触发事件 (源元素): ondragstart - 用户开始拖动元素时触发ondrag…

JS 事件 轮播图 0402

1. tab切换 <style>*{margin: 0;padding:0;}ul,ol,li{list-style: none;}.cont{width: 800px;height: 600px;border: 5px solid #333;margin: 0 auto;display: flex;flex-direction: column;}.cont ul{width: 100%;height: 60px;display: flex;}.cont ul li{flex:1;font…

[改善Java代码]推荐在复杂字符串操作中使用正则表达式

一、分析 字符串的操作&#xff0c;诸如追加、合并、替换、倒序、分隔等&#xff0c;都是在编码过程中经常用到的&#xff0c;而且Java也提供了append、replace、reverse、split等方法来完成这些操作&#xff0c;它们使用起来确实方便&#xff0c;但是更多的时候&#xff0c;需…

Python3的time模块的使用

import time# 1.time() print( python诞生总时间&#xff08;1970&#xff09;&#xff1a;, time.time())# 2.asctime() print(当前时间&#xff1a;, time.asctime()) # 当前时间# 3.ctime() print(当前时间&#xff1a;, time.ctime())# 4.gmtime() print(接收时间戳(格林威…