python 模块configparser

来源:互联网 发布:读音软件 编辑:程序博客网 时间:2024/06/03 14:11

模块的名称在 python 3.x 版本中变更为 configparser

实例:

import configparserconfig = configparser.ConfigParser()config["DEFAULT"]={    "ServiceAliveInterval":"45",    "Compression":"yes",    'CompressionLevel': '9'}config["bitbucket.org"]={    "user":"hg"}config['topsecret.server.com'] = {}topsecret = config['topsecret.server.com']topsecret["host port "] ="50022"topsecret["forwardx11"] ="no"config["DEFAULT"]["forwardx11"] = "yes"with open("file_configparser","w")as fp:    config.write(fp)print(config.sections())  #读取除了default之外的标题# for i in config["bitbucket.org"]:#     print(i)print(config["bitbucket.org"]["user"])
configparser增删改查语法

[section1]
k1 = v1
k2:v2
  
[section2]
k1 = v1
 
import ConfigParser
  
config = ConfigParser.ConfigParser()
config.read('i.cfg')
  
# ########## 读 ##########
#secs = config.sections()
#print secs
#options = config.options('group2')
#print options
  
#item_list = config.items('group2')
#print item_list
  
#val = config.get('group1','key')
#val = config.getint('group1','key')
  
# ########## 改写 ##########
#sec = config.remove_section('group1')
#config.write(open('i.cfg', "w"))
  
#sec = config.has_section('zongyimin')
#sec = config.add_section('zongyimin')
#config.write(open('i.cfg', "w"))
  
  
#config.set('group2','k1',11111)
#config.write(open('i.cfg', "w"))
  
#config.remove_option('group2','age')
#config.write(open('i.cfg', "w"))

0 0