python中configparser模块学习

 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section), 每个节可以有多个参数(键=值)。使用的配置文件的好处就是不用在程序员写死,可以使程序更灵活。

  

目录

三种创建方法
增删改查

三种创建方法

程序示例:

import configparser

#实例化出来一个类,相当于生成一个空字典
config = configparser.ConfigParser() #创建也很简单,键:值
# 值:键---值
#第一种方法
config['default']={'IP':'192.168.14.2',
'PORT':'6072'
} #第二种方法
config['Custom']={}
config['Custom']['User']='admin'
config['Custom']['Password']='123456'

#第三种方法
config['define']={}
Config=config['define']
Config['Host']='192.168.14.2'
Config['Port']='611' with open('confile','w') as configfile:
#注意这里,是谁调用write方法,是config对象,不是文件对象
config.write(configfile)

  

运行结果:

[default]
ip = 192.168.14.2
port = 6072 [Custom]
user = admin
password = 123456 [define]
host = 192.168.14.2
port = 611

  

增删改查

import configparser
config = configparser.ConfigParser()
#读取配置文件
config.read('confile')
print('获取文件内所有的section:')
print(config.sections()) print('获得指定section下所有option:')
options=config.options('Custom')
print(options) print('---------------------------查')
print('获取指定option下的值:')
value1=config['Custom']['user']
print(value1)
value2=config.get('Custom','user')
print(value2)
# getint(section,option) 得到section中option的值,返回为int类型,还有相应的getboolean()和getfloat() 函数。 print('获取指定section下所有的键值对:')
items = config.items('default')
print(items) print('遍历键值对:')
for key in config['default']:
print(key) #下面都会改变文件,所以最后一步都要重新写入配置文件 print('---------------------------增') print('添加section:')
# config.add_section('key1') print('添加键值对:')
# config.set('key1','k1','123456') print('---------------------------改')
#如果需要修改配置文件里面的值,自行打开修改
print('---------------------------删') print('删除section:')
config.remove_section('key1') print('删除键值对:')
config.remove_option('key1','k1') #重新保存
config.write(open('confile','w'))

  

python中confIgparser模块学习的更多相关文章

  1. Python中ConfigParser模块应用

    Python中ConfigParser模块应用 Python的ConfigParser模块定义了3个对INI文件进行操作的类 RawConfigParser.ConfigParser和SafeConf ...

  2. python中configparser模块读取ini文件

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

  3. python中configparser模块

    python中的configparse模块的使用 主要用来解析一些常用的配置,比如数据配置等. 例如:有一个dbconfig.ini的文件 [section_db1] db = test_db1 ho ...

  4. python中configparser模块的使用

    configparser模块用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 首先要写一个如下所示的配置文件: [DEFAULT] serv ...

  5. python 之ConfigParser模块学习

    1.1 读取配置文件 -read(filename) 直接读取ini文件内容 -sections() 得到所有的section,并以列表的形式返回 -options(section) 得到该secti ...

  6. python中configparser模块记录

    python中用来读取配置文件,配置文件的格式相同于windows下的ini配置文件 一.常用函数 read(filename) #读取配置文件,直接读取ini文件内容 sections() #获取i ...

  7. Python中optionParser模块的使用方法[转]

    本文以实例形式较为详尽的讲述了Python中optionParser模块的使用方法,对于深入学习Python有很好的借鉴价值.分享给大家供大家参考之用.具体分析如下: 一般来说,Python中有两个内 ...

  8. Python中的模块介绍和使用

    在Python中有一个概念叫做模块(module),这个和C语言中的头文件以及Java中的包很类似,比如在Python中要调用sqrt函数,必须用import关键字引入math这个模块,下面就来了解一 ...

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

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

随机推荐

  1. 以前的 Delphi版本

                    Delphi 1 Delphi 2 Delphi 3 Delphi 4 Delphi 5 Delphi 6 Delphi 7 Delphi 8 Delphi 2005

  2. python - json/pickle

    # import json #将数据类型转换成字符串 # data = {"a":"123"} # a = json.dumps(data) # print(a ...

  3. 根据href给当前导航添加样式

    var href = window.location.href.split('/')[window.location.href.split('/').length-1].substr(0,20); i ...

  4. 【Python】zip文件密码破解

    掌握基础语法后,尝试使用python的zipfile模块练手. zipfile是Python里用来做zip格式编码的压缩和解压缩的. 这里将大体的思路分解成四段代码,逐一完善功能: 第一段代码:解压z ...

  5. Generative Adversarial Nets(原生GAN学习)

    学习总结于国立台湾大学 :李宏毅老师 Author: Ian Goodfellow • Paper: https://arxiv.org/abs/1701.00160 • Video: https:/ ...

  6. Windows系统FTP Shell

    ftp open 10.0.0.0.2 21101 user passwd ls cd pwd delete get /home/err.log Error.log put err.log /home ...

  7. springboot系列十一、redisTemplate和stringRedisTemplate对比、redisTemplate几种序列化方式比较

    一.redisTemplate和stringRedisTemplate对比 RedisTemplate看这个类的名字后缀是Template,如果了解过Spring如何连接关系型数据库的,大概不会难猜出 ...

  8. Ex 6_17 数量无限的硬币兑换问题_第七次作业

    子问题定义:定义一个数组b,大小比兑换价格的大小多一个元素,其中b[i]表示是否能用面值为x1,x2,x3,..,xn的硬币兑换价格i. 递归关系: 初值设定:设b[0]=true 求解顺序:按下标从 ...

  9. PHP共享内存详解

    前言 在PHP中有这么一族函数,他们是对UNIX的V IPC函数族的包装. 它们很少被人们用到,但是它们却很强大.巧妙的运用它们,可以让你事倍功半. 它们包括: 信号量(Semaphores) 共享内 ...

  10. 手机端的1px边框如何实现

    (1).把边框设置为absolute,使用after,定义宽度为1px(mixin.styl) (2).通过@media,判断不同的dpi,来改变相应的Y轴宽度(base.styl),定义公共clas ...