Python 处理 ini 文件 的模块

本文介绍了Python中处理ini配置文件的方法,包括文件的基本结构和模块的使用。通过ConfigParser模块,可以将ini文件视为嵌套字典进行读写操作,提供了一种方便的语法介绍和操作示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、ini 文件

  • ini 文件是 Initialization File 的缩写,即初始化文件
  • ini 文件示例
  • 中括号里面的部分称为 section
  • 每一个 section 内,都是 key=value 形成的键值对,key被称作 option 选项
  • 注意这里的 DEFAULT 是缺省 section 的名字,必须大写
[DEFAULT]
a = test

[mysql]
default-character-set=utf8

[mysqld]
datadir =/dbserver/data
port = 33060
character-set-server=utf8
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

2、configparser 模块

  • configparser 模块的 ConfigParser 类来进行操作 ini 文件
  • 可以将 section 当作 keysection 存储着键值对组成的字典,可以把 ini 文件当作一个嵌套的字典,默认使用的是有序字典

2.1 语法介绍

# Read and parse a filename or an iterable of filenames.
ConfigParser.read(self, filenames, encoding=None)

# Return a list of section names, excluding [DEFAULT]
ConfigParser.sections(self)

# Create a new section in the configuration.  Extends RawConfigParser.add_section by validating if the section name is a string.
ConfigParser.add_section(self, section)

# Indicate whether the named section is present in the configuration.
ConfigParser.has_section(self, section)

# Return a list of option names for the given section name.
ConfigParser.options(self, section)

# Check for the existence of a given option in a given section.
# If the specified `section' is None or an empty string, DEFAULT is assumed. If the specified `section' does not exist, returns False.
ConfigParser.has_option(self, section, option)

# Get an option value for a given section.
# If the key is not found and `fallback' is provided, it is used as a fallback value. `None' can be provided as a `fallback' value.
ConfigParser.get(
    self,
    section,
    option,
    *,
    raw=False,
    vars=None,
    fallback=<object object at 0x000002E8694D1FF0>,
)

ConfigParser.getint(
    self,
    section,
    option,
    *,
    raw=False,
    vars=None,
    fallback=<object object at 0x000002E8694D1FF0>,
    **kwargs,
)

ConfigParser.getfloat(
    self,
    section,
    option,
    *,
    raw=False,
    vars=None,
    fallback=<object object at 0x000002E8694D1FF0>,
    **kwargs,
)

ConfigParser.getboolean(
    self,
    section,
    option,
    *,
    raw=False,
    vars=None,
    fallback=<object object at 0x000002E8694D1FF0>,
    **kwargs,
)

# Return a list of (name, value) tuples for each option in a section.
ConfigParser.items(
    self,
    section=<object object at 0x000002E8694D1FF0>,
    raw=False,
    vars=None,
)

# Set an option.  Extends RawConfigParser.set by validating type and interpolation syntax on the value.
ConfigParser.set(self, section, option, value=None)

# Remove a file section.
ConfigParser.remove_section(self, section)

# Remove an option.
ConfigParser.remove_option(self, section, option)

# rite an .ini-format representation of the configuration state.
ConfigParser.write(self, fp, space_around_delimiters=True)

2.2 操作示例

In [339]: from configparser import ConfigParser

In [340]: cat test.ini
[DEFAULT]
a = test

[mysql]
default-character-set=utf8

[mysqld]
datadir =/dbserver/data
port = 33060
character-set-server=utf8
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

In [341]: from configparser import ConfigParser

In [
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值