前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >configparser 在python

configparser 在python

作者头像
py3study
发布2020-01-06 11:47:11
3630
发布2020-01-06 11:47:11
举报
文章被收录于专栏:python3python3

1.创建configparser文件

代码语言:javascript
复制
import configparser
    #导入模块
config = configparser.ConfigParser()  #注意大小写与()
config['DEFAULT'] = {'Server': '45', 'Compression': 'yes'}
config['server'] = {'deletehq':'0',
                    'localtime':'20180706',
                    'port':'22'}
config['system'] = {'market64':'xiadan1.exe',
                    'market128':'xiadan2.exe',
                    'market256':'xiadan3.exe' }
config['client'] = {}

with open('configTest.ini', 'w') as configfile:
    config.write(configfile)

或者通过字典创建

代码语言:javascript
复制
config = configparser.ConfigParser()
config.read_dict(   {
                'section1': {'key1': 'value1',
                                    'key2': 'value2',
                                    'key3': 'value3'},
                'section2': {'keyA': 'valueA',
                                 'keyB': 'valueB',
                                 'keyC': 'valueC'},
                 'section3': {'foo': 'x',
                                    'bar': 'y',
                                  'fun: 'z'}
                                    }   )
with open('configTest1.ini', 'w') as configfile:
    config.write(configfile)

2.读取配置文件

代码语言:javascript
复制
config.read('configTest.ini')
代码语言:javascript
复制
[DEFAULT]
server = 45
compression = yes

[server]
deletehq = 0
localtime = 20180706
port = 22

[system]
market64 = xiadan1.exe
market128 = xiadan2.exe
market256 = xiadan3.exe

[client]

3.读取操作

代码语言:javascript
复制
获取所有sections
print(config.sections())
['server', 'system', 'client']

获取指定 section 的 keys & values
print(config.items('system'))
[('server', '45'), ('compression', 'yes'), ('market64', 'xiadan1.exe'), ('market128', 'xiadan2.exe'), ('market256', 'xiadan3.exe')]  # 注意items()返回的字符串会全变成小写

获取指定 section 的 keys
print(config.options('system'))
['market64', 'market128', 'market256', 'server', 'compression'] #会打印default中的keys

获取指定 key 的 value
print(config['system']['market64'])
xiadan1.exe

4.检查

代码语言:javascript
复制
‘section’ in config

‘option’ in config['section']

config.has_section['section']
config.hais_option['section','option']

5.添加

代码语言:javascript
复制
config.add_section('section4') 
config.set('section4','key1','value1')
config.write(open('configTest1.ini','w'))  #写入

[section4]
key1 = value1

6.删除

代码语言:javascript
复制
config.remove_option('section4','key1') #删除option
config.remove_section('section4') #删除section

7.[DEFAULT] [DEFAULT] 一般包含 ini 格式配置文件的默认项,所以 configparser 部分方法会自动跳过这个 section 。 sections() 是获取不到的,还有删除方法对 [DEFAULT] 也无效,但指定删除和修改 [DEFAULT] 里的 keys & values 是可以的,还有个特殊的是,has_section() 也无效,可以和 in 区别使用。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档