广告
返回顶部
首页 > 资讯 > 后端开发 > Python >configparser 在python
  • 152
分享到

configparser 在python

configparserpython 2023-01-31 08:01:34 152人浏览 薄情痞子

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

摘要

1.创建configparser文件 import configparser #导入模块 config = configparser.ConfigParser() #注意大小写与() config['DEFAULT'] = {'S

1.创建configparser文件

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)

或者通过字典创建

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.读取配置文件

config.read('configTest.ini')
[DEFAULT]
server = 45
compression = yes

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

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

[client]

3.读取操作

获取所有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.检查

‘section’ in config

‘option’ in config['section']

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

5.添加

config.add_section('section4') 
config.set('section4','key1','value1')
config.write(open('configTest1.ini','w'))  #写入

[section4]
key1 = value1

6.删除

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 区别使用。

--结束END--

本文标题: configparser 在python

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

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

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

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

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

  • 微信公众号

  • 商务合作