python 模块configparser   配置文件模块

import configparser

  
config = configparser.ConfigParser()
config["DEFAULT"= {'ServerAliveInterval''45',
           'Compression''yes',
           'CompressionLevel''9'}
  
config['bitbucket.org'= {'User': 'hg'}
config['topsecret.server.com'= {'Host Port':'50022',
                  'ForwardX11','no'}
with open('example.ini''w') as configfile:       #把键值对写入 配置文件example.ini
   config.write(configfile)
 
结果:
[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes [bitbucket.org]
User = hg [topsecret.server.com]
Port = 50022
ForwardX11 = no
 
config.read('example.ini')  # 关联文件才能操作里面键值
print(config.sections())   #['bitbucket.org', 'topsecret.server.com'] 查看段

print('bytebong.com' in config)# False 判断

print(config['bitbucket.org']['User']) # hg 查看值

print(config['DEFAULT']['Compression']) #yes 查看值

print(config['topsecret.server.com']['ForwardX11'])  #no 查看值
for key in config['bitbucket.org']:  #循环打印[bitbucker.org]的键
print(key)
###打印段[bitbucker.org]的所有键
print(config.options('bitbucket.org'))#['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11']
###循环打印段[bitbucker.org]的所有键值
print(config.items('bitbucket.org')) #[('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'hg')]

###打印段[bitbucket.org] 键compression 的值
print(config.get('bitbucket.org','compression'))#yes

config.add_section('yuan') #增加一个段 [yuan]
config.remove_section('topsecret.server.com')  # 删除一个段[topsecret.server.com]
config.remove_option('bitbucket.org','user')   # 删除这个[bitbucket.org]段里面的键和值User = hg
config.set('bitbucket.org','k1','11111')       # 添加[bitbucket.org]段里面 键值 k1 = 11111

config.write(open('i.cfg', "w")                # 写入的话 必须先执行这条命令  


python 模块之-configparser的更多相关文章

  1. Python模块之: ConfigParser 配置文件读取

    Python模块之: ConfigParser 配置文件读取   ConfigParser用于读写类似INI文件的配置文件,配置文件的内容可组织为组,还支持多个选项值(option-value)类型. ...

  2. Python模块学习 - ConfigParser

    配置文件 很多软件都用到了配置文件,像git运行的时候会读取~/gitconfig,MySQL运行的时候会读取/etc/my.cnf,Python 提供的包管理工具pip命令,也会去读取~/.pip/ ...

  3. Python模块之ConfigParser - 读写配置文件

    Python 标准库的 ConfigParser 模块提供一套 API 来读取和操作配置文件. 配置文件的格式 a) 配置文件中包含一个或多个 section, 每个 section 有自己的 opt ...

  4. Python 模块续 configparser、shutil、XML、paramiko、系统命令、

    一.configparse # 注释1 ; 注释2 [section1] # 节点 k1 = v1 # 值 k2:v2 # 值 [section2] # 节点 k1 = v1 # 值 1.获取所有节点 ...

  5. Python模块:configparser、hashlib、(subprocess)

    configparser模块: 此模块用于生成和修改常见配置文档. 一个常见配置文件(.ini的后缀名)格式如下: [DEFAULT] # DEFAULT 是指后面的字典里都会默认有的内容 Serve ...

  6. python模块之configparser

    configparser用于处理特定格式的文件,其本质上是利用open来操作文件. # 注释1 ; 注释2 [section1] # 节点 k1 = v1 # 值 k2:v2 # 值 [section ...

  7. python模块:configparser

    """Configuration file parser. A configuration file consists of sections, lead by a &q ...

  8. python模块之ConfigParser: 用python解析配置文件

    在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是ConfigParser,这里简单的做一些介 ...

  9. Python 模块之 ConfigParser: 用 Python 解析配置文件

    在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在 Python 里更是如此,在官方发布的库中就包含有做这件事情的库,那就是 ConfigParser,这里简单的做 ...

随机推荐

  1. #ifdef __cplusplus extern "C" { #endif”的定义

      平时我们在linux c平台开发的时候,引用了一些Cpp或者C的代码库,发现一些头文件有如下代码条件编译. #ifdef __cplusplus extern "C" { #e ...

  2. Skyline 二次实现单体化模型选择查询示例代码

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.or ...

  3. Topographic ICA as a Model of Natural Image Statistics(作为自然图像统计模型的拓扑独立成分分析)

    其实topographic independent component analysis 早在1999年由ICA的发明人等人就提出了,所以不算是个新技术,ICA是在1982年首先在一个神经生理学的背景 ...

  4. Robot Framework的日期处理

    http://www.cnblogs.com/channy14/p/6160831.html http://blog.csdn.net/r455678/article/details/52993765

  5. [Spark][Python]PageRank 程序

    PageRank 程序: file contents: page1 page3page2 page1page4 page1page3 page1page4 page2page3 page4 def c ...

  6. 【Qt】窗口居中显示

    w.move((a.desktop()->width() - w.width())/, (a.desktop()->height() - w.height())/); 上述方法可以置中,但 ...

  7. LOJ #6074. 「2017 山东一轮集训 Day6」子序列

    #6074. 「2017 山东一轮集训 Day6」子序列 链接 分析: 首先设f[i][j]为到第i个点,结尾字符是j的方案数,这个j一定是从i往前走,第一个出现的j,因为这个j可以代替掉前面所有j. ...

  8. 微信小程序:java后台获取openId

    一.功能描述 openId是某个微信账户对应某个小程序或者公众号的唯一标识,但openId必须经过后台解密才能获取(之前实现过前台解密,可是由于微信小程序的种种限制,前台解密无法在小程序发布后使用) ...

  9. HTTP Error 500.22 - Internal Server Error 错误解决方案

    1. 首先进入IIS ,配置IIS 应用程序池的.Net Framework版本 2. 点击左侧应用程序池,再单机右侧设置,选择版本 3. 设置为经典模式 如若遇到以下错误: 解决方案:删除confi ...

  10. Ceph分布式存储集群-硬件选择

    在规划Ceph分布式存储集群环境的时候,对硬件的选择很重要,这关乎整个Ceph集群的性能,下面梳理到一些硬件的选择标准,可供参考: 1)CPU选择Ceph metadata server会动态的重新分 ...