别院牧志知识库 别院牧志知识库
首页
  • 基础

    • 全栈之路
    • 😎Awesome资源
  • 进阶

    • Python 工匠系列
    • 高阶知识点
  • 指南教程

    • Socket 编程
    • 异步编程
    • PEP 系列
  • 面试

    • Python 面试题
    • 2025 面试记录
    • 2022 面试记录
    • 2021 面试记录
    • 2020 面试记录
    • 2019 面试记录
    • 数据库索引原理
  • 基金

    • 基金知识
    • 基金经理
  • 细读经典

    • 德隆-三个知道
    • 孔曼子-摊大饼理论
    • 配置者说-躺赢之路
    • 资水-建立自己的投资体系
    • 反脆弱
  • Git 参考手册
  • 提问的智慧
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
首页
  • 基础

    • 全栈之路
    • 😎Awesome资源
  • 进阶

    • Python 工匠系列
    • 高阶知识点
  • 指南教程

    • Socket 编程
    • 异步编程
    • PEP 系列
  • 面试

    • Python 面试题
    • 2025 面试记录
    • 2022 面试记录
    • 2021 面试记录
    • 2020 面试记录
    • 2019 面试记录
    • 数据库索引原理
  • 基金

    • 基金知识
    • 基金经理
  • 细读经典

    • 德隆-三个知道
    • 孔曼子-摊大饼理论
    • 配置者说-躺赢之路
    • 资水-建立自己的投资体系
    • 反脆弱
  • Git 参考手册
  • 提问的智慧
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 辨析

  • Sockets编程

  • Django

  • stackoverflow

  • Flask

  • 全栈之路

    • 基础篇

    • 进阶篇

    • 面向对象

    • 网络编程

    • 操作系统

    • 标准库

      • Python 标准库系列之 collections 模块
      • Python 标准库系列之 configparser 模块
        • 实例
      • Python 标准库系列之 datetime 模块
      • Python 标准库系列之 hashlib 模块
      • Python 标准库系列之模块介绍
      • Python 标准库系列之 json 模块
      • Python 标准库系列之 logging 模块
      • Python 标准库系列之 os 模块
      • Python 标准库系列之 random 模块
      • Python 标准库系列之 shutil 模块
      • Python 标准库系列之 subprocess 模块
      • Python 标准库系列之 sys 模块
      • Python 标准库系列之 tarfile 模块
      • Python 标准库系列之 time 模块
      • Python 标准库系列之 xml 模块
      • Python 标准库系列之 zipfile 模块
    • 第三方库

    • Python 全栈之路
  • 面试

  • 代码片段

  • 异步编程

  • 😎Awesome资源

  • PEP

  • Python工匠系列

  • 高阶知识点

  • Python 学习资源待整理
  • 设计模式

  • 好“艹蛋”的 Python 呀!
  • FIFO | 待学清单📝
  • pip 安装及使用
  • 数据分析

  • 源码阅读计划

  • OOP

  • 关于 python 中的 setup.py
  • 并行分布式框架 Celery
  • 七种武器,让你的代码提高可维护性
  • 使用 pdb 调试 Python 代码
  • 每周一个 Python 标准库
  • 🐍Python
  • 全栈之路
  • 标准库
佚名
2020-05-23
目录

Python 标准库系列之 configparser 模块

This module provides the ConfigParser class which implements a basic configuration language which provides a structure similar to what’s found in Microsoft Windows INI files. You can use this to write Python programs which can be customized by end users easily.

configparser 用于处理特定格式的文件,其本质上是利用 open 来操作文件。

配置文件格式如下:

# 第一种注释方式
; 第二种注释方式
 
[node1]  # 节点
k1 = v1  # key = value
k2 : v2  # key : value
1
2
3
4
5
6

# 实例

创建一个file.conf文件,内容为空,然后进入pythonIDE:

[root@ansheng ~]# touch file.conf 
[root@ansheng ~]# python
Python 2.6.6 (r266:84292, Jul 23 2016, 15:22:56) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
1
2
3
4
5
6

为文件添加节点

>>> import configparser
>>> config = configparser.ConfigParser()
>>> config.read('file.conf', encoding='utf-8')
['file.conf']
# 添加节点"node1","node2",然后写入文件
>>> config.add_section("node1")
>>> config.add_section("node2")
>>> config.write(open('file.conf', 'w'))
1
2
3
4
5
6
7
8

检查节点是否存在

# 如果文件存在则返回"True",否则就返回"False"
>>> print(config.has_section('node1'))
True
>>> print(config.has_section('node2'))
True
>>> print(config.has_section('node3'))
False
1
2
3
4
5
6
7

删除节点

# 如果删除的节点存在则返回"True",否则返回"False"
>>> config.remove_section("node2")
True
>>> config.write(open('file.conf', 'w'))
>>> print(config.has_section('node2'))
False
1
2
3
4
5
6

设置节点内的键值对

# 添加完键值对之后别忘记了写入到文件中
>>> config.set('node1', 'Name', "ansheng")
>>> config.set('node1', 'Blog_URL', "https://blog.ansheng.me")
>>> config.set('node1', 'Hostname', "localhost.localhost")
>>> config.set('node1', 'IP', "127.0.0.1")
>>> config.write(open('file.conf', 'w'))
1
2
3
4
5
6

检查节点内的 key 是否存在

# 如果节点的Key存在就返回"True",否则返回"False"
>>> print(config.has_option('node1', 'Name'))
True
>>> print(config.has_option('node1', 'IP'))
True
>>> print(config.has_option('node1', 'VV'))
False
1
2
3
4
5
6
7

删除节点内的 key

# 如果删除的节点存在就返回"True",否则就返回"False"
>>> config.remove_option('node1', 'IP')
True
>>> config.write(open('file.conf', 'w'))
>>> print(config.has_option('node1', 'IP'))
False
1
2
3
4
5
6

获取指定节点下指定 key 的值

# 默认返回的是字符串类型
>>> config.get('node1', 'Name')
'ansheng'
>>> config.get('node1', 'Blog_URL')
'https://blog.ansheng.me'
# 返回的字符串我们可以设置成一下三种数据类型,分别是"int","float","bool"
# v = config.getint('node1', 'k1')
# v = config.getfloat('node1', 'k1')
# v = config.getboolean('node1', 'k1')
1
2
3
4
5
6
7
8
9

获取指定节点下所有的 key

# 返回节点下面所有的Key列表
>>> config.options('node1')
['name', 'blog_url', 'hostname']
1
2
3

获取指定节点下所有的键值对

# 返回一个列表,列表中每个元组就是一个键值对
>>> config.items('node1')
[('name', 'ansheng'), ('blog_url', 'https://blog.ansheng.me'), ('hostname', 'localhost.localhost')]
1
2
3

获取所有节点

# 获取当前文件中有多少个节点
>>> config.sections()
['node1']
1
2
3
编辑 (opens new window)
#编码#面向对象
上次更新: 2024-07-23, 01:00:43
Python 标准库系列之 collections 模块
Python 标准库系列之 datetime 模块

← Python 标准库系列之 collections 模块 Python 标准库系列之 datetime 模块→

最近更新
01
2025 面试记录
05-28
02
提升沟通亲和力的实用策略
03-26
03
工作
07-15
更多文章>
Theme by Vdoing | Copyright © 2019-2025 IMOYAO | 别院牧志
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式