configParse模块(二十七)

configparser用于处理特定格式的文件,其本质上是利用open来操作文件。

# 注释1
;  注释2
 
[section1] # 节点
k1 = v1    #
k2:v2       #
 
[section2] # 节点
k1 = v1    #

指定格式

生成.ini

import configparser

config = configparser.ConfigParser()
config["DEFAULT"] = {'ServerAliveInterval':'45',
                       'Compression':'yes',
                        'CompressionLevel':'9'
                     }
config['bitbucket.org'] = { }
config['bitbucket.org']['User'] = 'abc'
config['topsecret.server.com'] = { }
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '50022'
topsecret['ForwardX11'] = 'no'
config["DEFAULT"]['ForwardX11'] = 'yes'

with open('example.ini','w') as configfile:
    config.write(configfile)
[DEFAULT]
compression = yes
serveraliveinterval = 45
compressionlevel = 9
forwardx11 = yes

[bitbucket.org]
user = abc

[topsecret.server.com]
host port = 50022
forwardx11 = no

读取

import configparser

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

# 查看所有标题
res = config.sections()
print(res) # ['bitbucket.org', 'topsecret.server.com']

# 查看标题section下所有的key=value的key ,DEFAULT 的key会在每一个子项中出现
options = config.options('bitbucket.org')
print(options) # ['user', 'passwd', 'compression', 'serveraliveinterval', 'compressionlevel', 'forwardx11']

# 查看标题section1下所有key=value的(key,value)格式
item_list=config.items('bitbucket.org')
print(item_list)
# [('compression', 'yes'), ('serveraliveinterval', '45'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'abc'), ('passwd', '123')]

# 查看标题section1下user的值=>字符串格式
val = config.get('bitbucket.org','user')
print(val) # abc

# 查看标题section1下passwd的值=>整数格式
val1 = config.getint('bitbucket.org','passwd')
print(val1) # 123

# 查看标题section1下is_admin的值=>布尔值格式
val2=config.getboolean('bitbucket.org','is_admin')
print(val2) # True

# 查看标题section1下salary的值=>浮点型格式
val3=config.getfloat('bitbucket.org','salary')
print(val3) # 31.0
View Code
import configparser

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

# 查看所有标题
res = config.sections()
print(res) # ['bitbucket.org', 'topsecret.server.com']

# 查看标题section下所有的key=value的key ,DEFAULT 的key会在每一个子项中出现
options = config.options('bitbucket.org')
print(options) # ['user', 'passwd', 'compression', 'serveraliveinterval', 'compressionlevel', 'forwardx11']

# 查看标题section1下所有key=value的(key,value)格式
item_list=config.items('bitbucket.org')
print(item_list)
# [('compression', 'yes'), ('serveraliveinterval', '45'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'abc'), ('passwd', '123')]

# 查看标题section1下user的值=>字符串格式
val = config.get('bitbucket.org','user')
print(val) # abc

# 查看标题section1下passwd的值=>整数格式
val1 = config.getint('bitbucket.org','passwd')
print(val1) # 123

# 查看标题section1下is_admin的值=>布尔值格式
val2=config.getboolean('bitbucket.org','is_admin')
print(val2) # True

# 查看标题section1下salary的值=>浮点型格式
val3=config.getfloat('bitbucket.org','salary')
print(val3) # 31.0
View Code

改写

 
import configparser

config = configparser.ConfigParser()
config.read('example.ini',encoding='utf-8')

# 删除整个标题section2
config.remove_section('section2')

# 删除标题section1下的某个key
config.remove_option('section1','salary')
config['section1']['is_admin'] = 'False'
config.set('section1','passwd','66666')

# 判断是否存在某个标题
print(config.has_section('section1')) # True

# 判断标题section1下是否有user
print(config.has_option('section1','user')) # True


# 添加一个标题
config.add_section('egon')

# 在标题egon下添加name=egon,age=18的配置
config.set('egon','name','egon')
#config.set('egon','age',18) #报错,必须是字符串
config.set('egon','age','18')

#最后将修改的内容写入文件,完成最终的修改
config.write(open('a.cfg','w'))

猜你喜欢

转载自www.cnblogs.com/xiangtingshen/p/10447459.html
今日推荐