.ini文件由若干section(部分)组成, 而每一个section又由若干键值对组成。

以 example.ini为例:

[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes [bitbucket.org]
User = hg [topsecret.server.com]
Port = 50022
ForwardX11 = no

创建.ini文件

import configparser

# configd对象类似于字典
config = configparser.ConfigParser()
config["DEFAULT"] = {
"ServerAliveInterval" : 45,
"Comperssion": "yes",
"ComperssionLevel": 9,
"ForwardX11": "yes"
} config["bitbucket.org"] = {}
config["bitbucket.org"]["user"] = "hg" config["topsecret.server.com"] = {}
config["topsecret.server.com"]["Port"] = ""
config["topsecret.server.com"]["ForwardX11"] = "no" with open("example.ini", "w") as fp:
# 写入文件
config.write(fp)

读取.ini文件

config = configparser.ConfigParser()
config.read("example.ini")

关于section

import configparser

config = configparser.ConfigParser()
config.read("example.ini")
# 获取section名称列表
print(config.sections()) # ['bitbucket.org', 'topsecret.server.com']
# 获取默认section名称
print(config.default_section) # DEFAULT
# 获取默认section对象
print(config.defaults()) # OrderedDict([('serveraliveinterval', '45'), ('comperssion', 'yes'), ('comperssionlevel', '9'), ('forwardx11', 'yes')]) # 判断是否包含对应的section
print('bitbucket.org' in config)
print(config.has_section("example.com")) # 添加section的2种方式
config['example.com'] = {}
config['example.com']['username'] = 'python'
config.add_section("example.com1") config.write(open("tmp.ini", "w")) # 删除section的2种方式
del config['example.com']
config.remove_section("example.com")
config.write(open("tmp.ini", "w"))

关于option

import configparser

config = configparser.ConfigParser()
config.read("example.ini") # 这里的option于key对应
# 获取section对应的key列表
bitbucket_keys = config.options('bitbucket.org')
print(bitbucket_keys) # ['user', 'serveraliveinterval', 'comperssion', 'comperssionlevel', 'forwardx11'] # 判断section中是否存在对应的key
print(config.has_option("bitbucket.org", "User"))
print('user1' in config.options('bitbucket.org'))
print('user1' in config['bitbucket.org']) # 添加option
config['bitbucket.org']['pwd'] = 'python'
config.write(open("tmp.ini", "w")) # 删除option的2种方式
del config['bitbucket.org']['User']
config.remove_option("bitbucket.org", "User")
config.write(open("tmp.ini", "w"))

value相关

import configparser

config = configparser.ConfigParser()
config.read("example.ini") # 获取key对应的value的2种方式
user = config['bitbucket.org']['User']
user = config.get('bitbucket.org', 'User')
print(user) # hg interval = config.getint("topsecret.server.com", "Port")
print(interval) #
compression = config.getboolean("topsecret.server.com", "ForwardX11")
print(compression)
# 还有 config.getfloat() # 设置value的2种方式
config.set('bitbucket.org', 'User', 'peter')
config['bitbucket.org']["User"] = "peter"
config.write(open("tmp.ini", "w"))

python configparser使用的更多相关文章

  1. python configparser模块

    来看一个好多软件的常见文档格式如下: [DEFAULT] ServerAliveInterval = 45 Compression = yes CompressionLevel = 9 Forward ...

  2. python ConfigParser配置读写

    一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号"[ ]"内包含的为section.section 下面为类似于key ...

  3. python ConfigParser、shutil、subprocess、ElementTree模块简解

    ConfigParser 模块 一.ConfigParser简介ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.section 下面为类 ...

  4. [Python]ConfigParser解析配置文件

    近期发现非常多接口配置都硬编码在souce file中了,于是就看了下python怎么解析配置文件,重构下这一块. 这个应该是早就要作的... 配置文件: [mysqld] user = mysql ...

  5. Python configparser 读取指定节点内容失败

    # !/user/bin/python # -*- coding: utf-8 -*- import configparser # 生成一个config文件 config = configparser ...

  6. Python Configparser模块读取、写入配置文件

    写代码中需要用到读取配置,最近在写python,记录一下. 如下,假设有这样的配置. [db] db_host=127.0.0.1 db_port=3306 db_user=root db_pass= ...

  7. Python ConfigParser的使用

    1.基本的读取配置文件 -read(filename) 直接读取ini文件内容 -sections() 得到所有的section,并以列表的形式返回 -options(section) 得到该sect ...

  8. python ConfigParser读取配置文件,及解决报错(去掉BOM)ConfigParser.MissingSectionHeaderError: File contains no section headers的方法

    先说一下在读取配置文件时报错的问题--ConfigParser.MissingSectionHeaderError: File contains no section headers 问题描述: 在练 ...

  9. 【转载】Python ConfigParser的使用

    1.基本的读取配置文件-read(filename) 直接读取ini文件内容-sections() 得到所有的section,并以列表的形式返回-options(section) 得到该section ...

随机推荐

  1. laravel进行单元测试的时候如何模拟数据库以及mockery的调用

    单元测试是独立的,所谓的独立是指有独立的运行容器,独立的数据库. 这样做有什么好处呢? (1). 不会跟正常的容器产生冲突,继而影响正常业务. (2). 数据库独立防止数据被修改影响单元测试结果. 这 ...

  2. 『Python CoolBook:Collections』数据结构和算法_collections.deque队列&yield应用

    一.collections.deque队列 deque(maxlen=N)构造函数会新建一个固定大小的队列.当新的元素加入并且这个队列已满的时候,最老的元素会自动被移除掉. 如果你不设置最大队列大小, ...

  3. vue 打开新页面

    <router-link tag="a" target="_blank" :to="{path:'/system/detail?id=' + s ...

  4. vue教程自学笔记(三)

    五.Class与Style绑定 可以用v-bind用于class和style,表达式结果类型除了字符串,还可以是对象或数组. 1.绑定HTML Class 对象语法:给v-bind:class传递一个 ...

  5. static_assert enable_if 模板编译期检查

    conceptC++ http://www.generic-programming.org/faq/?category=conceptcxx Checking Concept Without Conc ...

  6. Typescript中的装饰器原理

    Typescript中的装饰器原理 1.小原理 因为react中的高阶组件本质上是个高阶函数的调用, 所以高阶组件的使用,我们既可以使用函数式方法调用,也可以使用装饰器. 也就是说,装饰器的本质就是一 ...

  7. LeetCode 257 二叉树的所有路径

    题目: 给定一个二叉树,返回所有从根节点到叶子节点的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 输入: 1 / \ 2 3 \ 5 输出: ["1->2->5&quo ...

  8. 用matlab生成mif文件

    在FPGA中,rom模块的初始化文件分为两种,一种是hex文件,另外一种是mif文件,这两种文件都可以通过Quartus进行手工创建,进行手工输入数据,也可以借助于专用的文件编辑器完成编辑. 在此介绍 ...

  9. cython 成功创建import 模块

    又是因为别人代码里有这么一个部分,用到了cython,,简而言之,就是利用这个模块调用C语言,从而加速程序运行,其中具体怎么调用我还没整清楚,但基本用法差不多了解了. 1 安装:https://www ...

  10. 新建一个self hosted Owin+ SignalR Project(1)

    OWIN是Open Web Server Interface for .Net 的首字母缩写,他的定义如下: OWIN在.NET Web Server 与Web Application之间定义了一套标 ...