iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python中ConfigParser模块示例详解
  • 186
分享到

Python中ConfigParser模块示例详解

Python中ConfigParser模块Python中ConfigParser 2023-01-15 12:01:11 186人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

目录1. 简介2. ini配置文件格式3. 读取ini文件3.1 初始化对象并读取文件3.2 获取并打印所有节点名称3.3 获取指定节点的所有key3.4 获取指定节点的键值对3.5

1. 简介

有些时候在项目中,使用配置文件来配置一些灵活的参数是比较常见的事,因为这会使得代码的维护变得更方便。而ini配置文件是比较常用的一种,今天介绍用ConfigParser模块来解析ini配置文件。

2. ini配置文件格式

# 这是注释
; 这也是注释
[section1]
name = wang
age = 18
heigth = 180

[section2]
name = python
age = 19

3. 读取ini文件

configparser模块为Python自带模块不需要单独安装,但要注意,在python3中的导入方式与Python2的有点小区别

# python2
import ConfigParser

# python3
import configparser

3.1 初始化对象并读取文件

import configparser
import os
# 创建对象
config = configparser.ConfigParser()
dirPath = os.path.dirname(os.path.realpath(__file__))
inipath = os.path.join(dirPath,'test.ini')
# 读取配置文件,如果配置文件不存在则创建
config.read(inipath,encoding='utf-8')

3.2 获取并打印所有节点名称

secs = config.sections()
print(secs)

输出结果:

['section1', 'section2']

3.3 获取指定节点的所有key

option = config.options('section1')
print(option)

输出结果:

['name', 'age', 'heigth']

3.4 获取指定节点的键值对

item_list = config.items('section2')
print(item_list)

输出结果:

[('name', 'python'), ('age', '19')]

3.5 获取指定节点的指定key的value

val = config.get('section1','age')
print('section1的age值为:',val)

输出结果:

section1的age值为: 18

3.6 将获取到值转换为int\bool\浮点型

Attributes = config.getint('section2','age')
print(type(config.get('section2','age')))
print(type(Attributes))

# Attributes2 = config.getboolean('section2','age')
# Attributes3 = config.getfloat('section2','age')

输出结果:

<class 'str'>
<class 'int'>

3.7 检查section或option是否存在,返回bool值

has_sec = config.has_section('section1')
print(has_sec)

has_opt = config.has_option('section1','name')
print(has_opt)

输出结果:

TrueTrue

3.8 添加一个section和option

if not config.has_section('node1'):
    config.add_section('node1')

# 不需判断key存不存在,如果key不存在则新增,若已存在,则修改value
config.set('section1','weight','100')  

# 将添加的节点node1写入配置文件
config.write(open(inipath,'w'))
print(config.sections())
print(config.options('section1'))

输出结果:

['section1', 'section2', 'node1']
[('name', 'wang'), ('age', '18'), ('heigth', '180'), ('weight', '100')]

3.9 删除section和option

# 删除option
print('删除前的option:',config.items('node1'))
config.remove_option('node1','dd')
# 将删除节点node1后的内容写回配置文件
config.write(open(inipath,'w'))
print('删除后的option:',config.items('node1'))

输出结果:

删除前的option: [('dd', 'ab')]
删除后的option: []

# 删除section
print('删除前的section: ',config.sections())
config.remove_section('node1')
config.write(open(inipath,'w'))
print('删除后的section: ',config.sections())

输出结果:

删除前的section:  ['section1', 'section2', 'node1']
删除后的section:  ['section1', 'section2']

3.10 写入方式

1、write写入有两种方式,一种是删除源文件内容,重新写入:w

config.write(open(inipath,'w'))

另一种是在原文基础上继续写入内容,追加模式写入:a

config.write(open(inipath,'a'))

需要注意的是,config.read(inipath,encoding='utf-8')只是将文件内容读取到内存中,即使经过一系列的增删改操作,只有执行了以上的写入代码后,操作过的内容才会被写回文件,才能生效。

到此这篇关于Python中ConfigParser模块详谈的文章就介绍到这了,更多相关Python中ConfigParser模块内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Python中ConfigParser模块示例详解

本文链接: https://www.lsjlt.com/news/177776.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作