生成配置文件

import configparser
config = configparser.ConfigParser()
config["DEFAULT"] = {'ServerAliveInterval': '',
'Compression': 'yes',
'CompressionLevel': '',
'ForwardX11':'yes'
}
config['bitbucket.org'] = {'User':'hg'} config['topsecret.server.com'] = {'Host Port':'','ForwardX11':'no'} with open('example.ini', 'w') as f:
config.write(f)
[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes [bitbucket.org]
user = hg [topsecret.server.com]
host port = 50022
forwardx11 = no

example.ini

普通操作

import configparser

config = configparser.ConfigParser()
# #---------------------------查找文件内容,基于字典的形式
print(config.sections()) # [] config.read('example.ini') # 读取文件指定文件
print(config.sections()) # ['bitbucket.org', 'topsecret.server.com'] 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)
"""
user
serveraliveinterval
compression
compressionlevel
forwardx11
""" 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
# yes

获取配置信息

高级操作

import configparser
config = configparser.ConfigParser()
config.read('example.ini') # 读文件
config.add_section('yuan') # 增加section
config.remove_section('bitbucket.org') # 删除一个section
config.remove_option('topsecret.server.com',"forwardx11") # 删除一个配置项
config.set('topsecret.server.com','k1','')
config.set('yuan','k2','')
f = open('new2.ini', "w")
config.write(f) # 写进文件
f.close() #这些操作不会修改example.ini的文件 因为是以读打开的文件

读取example.ini写入new2.ini

[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes [topsecret.server.com]
host port = 50022
k1 = 11111 [yuan]
k2 = 22222

new2.ini

Python开发【内置模块篇】configparser的更多相关文章

  1. 《python开发技术详解》|百度网盘免费下载|Python开发入门篇

    <python开发技术详解>|百度网盘免费下载|Python开发入门篇 提取码:2sby  内容简介 Python是目前最流行的动态脚本语言之一.本书共27章,由浅入深.全面系统地介绍了利 ...

  2. python开发第一篇:初识python

    一. Python介绍 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为AB ...

  3. Python开发第一篇

    Python 是什么? 首先他可能是比较好的一个编程开发语言!

  4. Python开发 第一篇 python的前世今生

    Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC ...

  5. python开发[第二篇]------str的7个必须掌握的方法以及五个常用方法

    在Python中 基本数据类型有 str int boolean list dict tuple等 其中str的相关方法有30多个 但是常用的就以下7个 join  # split # find # ...

  6. python开发第二篇 :python基础

    python基础a.Python基础      -基础1. 第一句python       -python后缀名可以任意?     -导入模块时如果不是.py文件,以后的文件后缀名是.py.2.两种 ...

  7. Python开发 基础篇

    2019-02-01 产生验证码: 用户输入的值和显示的值相同时显示Correct,否则继续生成随机验证码等待用户输入 def check_code(): import random checkcod ...

  8. Python开发第二篇

    运算符 1.算术运算符 % 取余运算符,返回余数 ** 幂运算符 //返回商的整数部分 2.逻辑运算符 and  与运算符 a and b 如果a为False是,表达式为False,如果a为True返 ...

  9. Python开发【第六篇】:模块

    模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...

  10. Python开发【第十篇】:模块

    模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...

随机推荐

  1. Linux下RabbitMQ服务器搭建

    系统环境 操作系统:CentOS6.9 erlang:OTP 19.3 rabbitmq:rabbitmq-server 3.6.12 安装步骤 安装erlang 1,安装预环境 通过yum安装以下组 ...

  2. 从锅炉工到AI专家(4)

    手写数字识别问题 图像识别是深度学习众多主流应用之一,手写数字识别则是图像识别范畴简化版的入门学习经典案例.在TensorFlow的官方文档中,把手写数字识别"MNIST"案例称为 ...

  3. .netcore使用SocketAsyncEventArgs Pool需要注意!

    在.net中做网络通讯往往都会用到SocketAsyncEventArgs,为了得到更好的性能配合Pool复用SocketAsyncEventArgs可以得到一个更好的效果,但在dotnet core ...

  4. HashMap扩容全过程

      1.如果HashMap的大小超过了负载因子(load factor)定义的容量,怎么办? 默认的负载因子大小为0.75,也就是说,当一个map填满了75%的bucket时候,和其它集合类(如Arr ...

  5. TypeScript 基础知识点整理

    一.TypeScript的特点 1.支持ES6规范 2.强大的IDE支持(集成开发环境) 允许为变量指定类型,减少你在开发阶段犯错误的几率. 语法提示,在IDE编写代码时,它会根据你所处的上下文把你能 ...

  6. centos7下vim8.1的编译安装教程

    之前安装YouCompleteMe的时候遇到vim版本不兼容的问题,看网上说是需要将vim版本提升到8.0及以上,然后就开始安装最新版本的vim,安装过程中的遇到了不少问题主要集中在配置方面和缺少插件 ...

  7. .Net Core SDK 命令介绍

    前言 本篇主要介绍 asp.net core 中,使用 dotnet tools 运行 dotnet run 之后的系统执行过程. 如果你觉得对你有帮助的话,不妨点个[推荐]. 目录 dotnet r ...

  8. 第24章 退出 - Identity Server 4 中文文档(v1.0.0)

    注销IdentityServer就像删除身份验证cookie一样简单,但是为了完成联合注销,我们必须考虑将用户从客户端应用程序(甚至可能是上游身份提供者)中签名. 24.1 删除认证 要删除身份验证c ...

  9. ElasticSearch6学习(1)-安装Elasticsearch

    安装准备: 安装Elasticsearch唯一的要求是安装Java8,包括对应的Jdk.其他java9/java10没尝试,考虑兼容问题.我这里只用了java8 安装java8可以参考我之前写的一篇文 ...

  10. 3. 基于优先级的Queue(PriorityBlockingQueue)

    package com.gf.conn013; import java.util.Iterator; import java.util.concurrent.PriorityBlockingQueue ...