一、概述

1.1、处理的文件形式

configparse 主要是用来处理类似于windows的 ini文件,这个文件的特点是有多个节(section),每个节下会存储多个k=v的值

如下配置

[hardware]
cpu = 2
[os]
os = CentOS
os_version = 6.9
kernel_version = 2.6.32
[common]
selinux = disabled
file_descriptor = 65535
[nginx]
version = 1.6.3
install_path = /tuike/server/nginx
config_file =['/middleWare/nginx/centralapi.conf','/middleWare/nginx/wezhuanba1.conf']

1.2、相关方法

  • config.write()   将配置写入文件,括号里是文件的句柄

二:实践

2.1、生成配置文件

import  configparser
config = configparser.ConfigParser() # 得到一个对象
config["hardware"] = {'cpu':''} # 增加一个hardware节,节内的内容使用字典数据格式表示
config["os"] = {'os':'CentOS','os_version':'6.9','kernel_version':'2.6.32'}
config['common'] = {'selinux':'disabled',
'file_descriptor':''
}
config['nginx'] = {'version':'1.6.3',
'install_path':'/tuike/server/nginx',
'config_file':['/middleWare/nginx/centralapi.conf','/middleWare/nginx/wezhuanba1.conf']
}
with open('test','w',encoding='utf-8') as f:
config.write(f)

2.2、各种操作

import configparser
config = configparser.ConfigParser() # 拿到一个配置对象 ############## sections 操作
# 判断sections是否再配置文件里
print('os' in config) # section in 配置对象
print('wangys' in config) # 查看sections
print(config.sections()) # 直接看sections,因为没有读取配置文件,所以会返回一个空列表
# []
config.read('test.ini',encoding='utf-8') # 读取配置文件,注意编码
print(config.sections()) # 再次打印sections,可以看到有结果
# ['hardware', 'os', 'common', 'nginx'] # 增加一个section
config.add_section('wangys')
print(config.sections())
# ['hardware', 'os', 'common', 'nginx', 'wangys'] # 删除一个section
config.remove_section('wangys')
print(config.sections())
# ['hardware', 'os', 'common', 'nginx'] ##### 操作k/v
# 查看某个节里的内容
# 使用 for 循环去key
# 判断某个key是否再selections里 for key in config['os']:
print(key) # options查看selctions下配置的key
print(config.options('nginx')) # 查看你k v 值 返回一个列表,没有k/v组成一个元祖
print(config.items('nginx'))
# [('version', '1.6.3'), ('install_path', '/tuike/server/nginx'), ('config_file', "['/middleWare/nginx/centralapi.conf', '/middleWare/nginx/wezhuanba1.conf']")]
# 获取某个节下的某个key的value值
print(config.get('os','os_version'))
# print(config['os']['os_version','kernel_version']) # 读取某个sections下的所有的k/v值
l = config.options('nginx')
for key in l:
print('%s:%s'%(key,config.get('nginx',key))) # 删除一个sections下的key config.remove_option('os','kernel_version')
print(config.options('os'))
#['os', 'os_version'] # 增加一个option
config.set('os','name','王诚')
print(config.options('os'))
print(config.get('os','name'))
# ['os', 'os_version', 'name']
# 王永胜 with open('test.ini','w',encoding='utf-8') as f:
config.write(f)

Python内置模块之configparse的更多相关文章

  1. Python学习笔记【第八篇】:Python内置模块

    什么时模块 Python中的模块其实就是XXX.py 文件 模块分类 Python内置模块(标准库) 自定义模块 第三方模块 使用方法 import 模块名 form 模块名 import 方法名 说 ...

  2. python内置模块(4)

    这一部分是python内置模块系列的最后一部分,介绍了一些小巧有用的内置模块. 目录: 1.random 2.shelve 3.getpass 4.zipfile 5.tarfile 6.bisect ...

  3. Python内置模块与标准库

    Python内置模块就是标准库(模块)吗?或者说Python的自带string模块是内置模块吗? 答案是:string不是内置模块,它是标准库.也就是说Python内置模块和标准库并不是同一种东西. ...

  4. python内置模块[re]

    python内置模块[re] re模块: python的re模块(Regular Expression正则表达式)提供各种正则表达式的匹配操作,在文本解析.复杂字符串分析和信息提取时是一个非常有用的工 ...

  5. python内置模块[sys,os,os.path,stat]

    python内置模块[sys,os,os.path,stat] 内置模块是python自带功能,在使用内置模块时,需要遵循 先导入在 使用 一.sys 对象 描述 sys.argv 命令行参数获取,返 ...

  6. Python内置模块和第三方模块

    1.Python内置模块和第三方模块 内置模块: Python中,安装好了Python后,本身就带有的库,就叫做Python的内置的库. 内置模块,也被称为Python的标准库. Python 2.x ...

  7. python内置模块collections介绍

    目录 python内置模块collections介绍 1.namedtuple 2.deque 3.defaultdict 4.OrderedDict 5.ChainMap 6.Counter 7.小 ...

  8. python内置模块介绍(一)

     本文主要介绍模块列表如下: os sys re time datetime random shutil subprocess os模块 os.getcwd()                    ...

  9. python内置模块(time模块)

    常用的python内置模块 一.time模块 在python的三种时间表现形式: 1.时间戳,给电脑看的. - 自1970-01-01 00:00:00到当前时间,按秒计算,计算了多少秒. impor ...

随机推荐

  1. Linux(Ubuntu)使用日记------vim复制内容到其他应用

    1.用vim 打开一个文件,然后执行命令:reg 查看是否有 + 或者 × 号  或者执行:version 命令 查看是否有+clipboard 2.如果存在跳过此步骤.如果不存在:在终端输入 sud ...

  2. Linux(Ubuntu)使用日记(三)------git安装使用

    1. 安装 首先,确认你的系统是否已安装git,可以通过git指令进行查看,如果没有,在命令行模式下输入sudo apt-get install git命令进行安装. 2.  配置 git confi ...

  3. Sqoop export(Hive to MySQL) 的一些 reference

    之后可能会整理成文章..还有一些坑没趟完. Reference: https://cloud.tencent.com/developer/article/1078473  Sqoop抽取Hive Pa ...

  4. Linux 系统中五笔输入法有些字打不出来(已解决)

    最近在使用CentOS7 桌面版本,在用五笔打字时,有些字打不出来,比如“覆盖”.但是在WIN下能打出来. 从网上查找原因,原来是需要改成GBK字符集.方法如下: 修改文件 vim /usr/shar ...

  5. Working with PDF files in C# using PdfBox and IKVM

    I have found two primary libraries for programmatically manipulating PDF files;  PdfBox and iText. T ...

  6. git 的简单实用

    一. 安装 Git(git_for_windows.xp510.com.rar) 二. 使用 a) 进入到 git bash(命令行工具) b) 初始化user.name,user.email $ g ...

  7. ElasticSsarch汇总

    用途: 分布式实时文件存储,并将每一个字段都编入索引,使其可以被搜索: 实时分析的分布式搜索引擎: 可以扩展到上百台服务器,处理PB级别的结构化或非结构化数据. 点击查看安装.基本增删改查操作REST ...

  8. FastDFS分布式文件系统客户端安装

    软件安装前提:服务器已配置好LNMP环境安装libfastcommon见FastDFS服务器安装文档(http://www.cnblogs.com/Mrhuangrui/p/8316481.html) ...

  9. 2019南昌邀请赛网络预选赛 I. Max answer(单调栈+暴力??)

    传送门 题意: 给你你一序列 a,共 n 个元素,求最大的F(l,r): F(l,r) = (a[l]+a[l+1]+.....+a[r])*min(l,r); ([l,r]的区间和*区间最小值,F( ...

  10. (二叉树 DFS 递归) leetcode 112. Path Sum

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...