Python模块——configparser
2024-09-11 20:08:36
configparser模块
该模块适用于配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键=值)
创建文件
import configparser config = configparser.ConfigParser() config["DEFAULT"] = {'ServerAliveInterval': '',
'Compression': 'yes',
'ForwardX11':'yes'
} config['bitbucket.org'] = {'User':'hg'} config['topsecret.server.com'] = {'Host Port':'','ForwardX11':'no'} with open('example.ini', 'w') as configfile: config.write(configfile)
文件内容
[DEFAULT]
serveraliveinterval = 45
compression = yes
forwardx11 = yes [bitbucket.org]
user = hg [topsecret.server.com]
host port = 9999
forwardx11 = no
查找文件内容
方法 | 描述 |
---|---|
defaults() | 返回包含实例范围默认值的字典。 |
sections() | 返回可用的section的列表;默认section不包括在列表中 |
has_section(section) | 指示指定的section是否出现在配置中。默认的section未被确认 |
options(section) | 返回指定section中可用的选项列表。 |
has_option(section, option) | 如果给定的section存在,并且包含给定的选项,则返回True;否则返回False |
get(section, option) | 为指定的section获取一个选项值。 |
getint(section, option) | 它将指定section中的选项强制转换为整数 |
getfloat(section, option) | 它将指定section中的选项强制转换为浮点型 |
getboolean(section, option) | 强制转换为布尔型,”1”, “yes”, “true”, and “on”, 转换为True,”0”, “no”, “false”, and “off”, 转换为Falseo 其他返回ValueError. |
items(section) | 返回给定section中每个选项的(name,value)对的列表。 |
import configparser config = configparser.ConfigParser() print(config.sections()) # []
# ---------------------------查找文件内容,基于字典的形式
config.read('example1.ini') # ['bitbucket.org', 'topsecret.server.com'] print(config.sections())
print('bytebong.com' in config) # False
print('bitbucket.org' in config) # True print(config['bitbucket.org']["user"]) # hg print(config['DEFAULT']['Compression']) #yes print(config['topsecret.server.com']['ForwardX11']) #no # 其他方式查找文件内容
print(config['bitbucket.org']) #<Section: bitbucket.org> for key in config['bitbucket.org']: # 注意,有default会默认default的键
print(key) print(config.options('bitbucket.org')) # 同for循环,找到'bitbucket.org'下所有键
# ['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11'] print(config.items('bitbucket.org')) #找到'bitbucket.org'下所有键值对
# [('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'hg')] print(config.get('bitbucket.org','compression')) # yes get方法Section下的key对应的value
print(config.get('topsecret.server.com','host port')) # for k, v in config.items('bitbucket.org'):
print(k, v)
"""
serveraliveinterval 45
compression yes
forwardx11 yes
user hg
"""
修改和删除文件内容
增加配置文件中的值
方法 | 描述 |
---|---|
add_section(section) | 向实例添加一个section |
删除配置文件中的值
方法 | 描述 |
---|---|
remove_option(section, option) | 从指定的部分中删除指定的选项。如果该部分不存在,请提出NoSectionError。如果存在的选项被删除,返回True;否则返回False。 |
remove_section(section) | 从配置中删除指定的section。如果这个部分确实存在,返回True。否则返回假 |
修改配置文件中的值
方法 | 描述 |
---|---|
set(section, option, value) | 如果给定的部分存在,将给定的选项设置为指定的值 |
import configparser config = configparser.ConfigParser() config.read('example.ini') config.add_section('db') # 增加 config.remove_section('bitbucket.org') # 删除
config.remove_option('topsecret.server.com',"forwardx11") # 删除 config.set('topsecret.server.com','k1','') # 修改
config.set('db','k2','') # 修改 config.write(open('new2.ini', "w"))
最新文章
- ionic imgBase64
- iOS项目目录结构
- Objective-c——UI基础开发第十一天(UICollectionView)
- Swift计算文本宽高
- 【阿里云产品公测】简单日志服务SLS使用评测 + 教程
- bootstrap时间插件 火狐不显示 完美解决方法
- HDU 2444 The Accomodation of Students(判断是否可图 + 二分图)
- VS2010无法断点调试解决办法
- MUI判断网络连接以及监听网络变化JS
- 使用NPOI生成Excel级联列表
- python--socket套接字/TCP
- [转]impala操作hive数据实例
- 判断ios还是android
- 剑指Offer 14. 链表中倒数第k个结点 (链表)
- LeanCloud云引擎相关问题
- 三、主流区块链技术特点及Hyperledger Fabric V1.0版本特点
- python3 连接HBase
- iOS正則表達式(一)
- ES6的新特性(12)——Set 和 Map 数据结构
- 简化delegate写法