python configparser
configparser模块操作
config.ini 配置文件
[user] user_name = root password = 1234 money = 26985.4578 [connect] ip = 127.0.0.1 port = 8888 [mysql] ip = 192.168.11.122 port = 3369 [mysql_connect] is_true = true is_false = false [redis] name = 192.168.1.22
py 文件
import configparser # 生成 ConfigParser对象 config = configparser.ConfigParser() # 读取配置文件 file_name = ‘config.ini‘ config.read(filenames=file_name, encoding=‘utf-8‘) # 获取所有节点sections, 以列表形式返回config parser对象的所有节点信息 all_sections = config.sections() print(all_sections) # [‘user‘, ‘connect‘, ‘mysql‘, ‘mysql_connect‘] # 获取指定节点的的所有配置信息,以列表形式返回某个节点section对应的所有配置信息 user = config.items(‘user‘) print(user) # [(‘user_name‘, ‘root‘), (‘password‘, ‘root‘)] # 获取指定节点的options, 以列表形式返回某个节点section的所有key值 user = config.options(‘user‘) print(user) # [‘user_name‘, ‘password‘, ‘money‘] # 获取指定节点下指定option的值 # ConfigParserObject.get(section, option) # 返回结果是字符串类型 name = config.get(‘user‘, ‘user_name‘) print(name, type(name)) # root <class ‘str‘> # ConfigParserObject.getint(section, option) # 返回结果是int类型 password = config.getint(‘user‘, ‘password‘) print(password, type(password)) # 1234 <class ‘int‘> # ConfigParserObject.getboolean(section, option) # 返回结果是bool类型 is_true = config.getboolean(‘mysql_connect‘, ‘is_true‘) print(is_true, type(is_true)) # True <class ‘bool‘> # ConfigParserObject.getfloat(section, option) # 返回结果是float类型 money = config.getfloat(‘user‘, ‘money‘) print(money, type(money)) # 26985.4578 <class ‘float‘> # 检查section或option是否存在, 返回bool值,若存在返回True,不存在返回False # 检查section是否存在 result = config.has_section(‘user_name‘) print(result) # False result = config.has_section(‘user‘) print(result) # True # 检查option是否存在 result = config.has_option(‘user‘, ‘user_name‘) print(result) # True result = config.has_option(‘user‘, ‘root‘) print(result) # False # 添加section # 如果section不存在,则添加节点section; # 若section已存在,再执行add操作会报错configparser.DuplicateSectionError: Section XX already exists if not config.has_section(‘redis‘): config.add_section(‘redis‘) config.set(‘redis‘, ‘name‘, ‘192.168.1.22‘) f = open(‘config.ini‘, ‘w‘) config.write(f) f.close() redis = config.items(‘redis‘) print(redis) # [(‘name‘, ‘192.168.1.22‘)], config.ini 会多出来增加的section # 修改或添加指定节点下指定option的值 # 若option存在,则会替换之前option的值为value, 若option不存在,则会创建option并赋值为value # 修改指定option的值 config.set(‘user‘, ‘user_name‘, ‘root‘) config.set(‘user‘, ‘user_name‘, ‘tingfeng‘) f = open(‘config.ini‘, ‘w‘) config.write(f) f.close() # 重新查看修改后节点信息 items = config.items(‘user‘) print(items) # [(‘user_name‘, ‘tingfeng‘), (‘password‘, ‘1234‘), (‘money‘, ‘26985.4578‘)] # 删除section或option # ConfigParserObject.remove_section(section) # 若section存在,执行删除操作,若section不存在,则不会执行任何操作 # 删除section config.remove_section(‘user‘) config.remove_section(‘user_name‘) all_sections = config.sections() print(all_sections) # [‘connect‘, ‘mysql‘, ‘mysql_connect‘, ‘redis‘] # ConfigParserObject.remove_option(section, option) # 若option存在,执行删除操作,若option不存在,则不会执行任何操作; # 若section不存在,则会报错configparser.NoSectionError: No section: XXX # 删除option config.remove_option(‘user‘, ‘user_name‘) config.remove_option(‘user‘, ‘user_name‘) f = open(‘config.ini‘, ‘w‘) config.write(f) f.close() all_sections = config.sections() print(all_sections) # [‘user‘, ‘connect‘, ‘mysql‘, ‘mysql_connect‘, ‘redis‘] print(config.options(‘user‘)) # [‘password‘, ‘money‘] # 写入配置文件 # 对configparser对象执行的一些修改操作,必须重新写回到文件才可生效 config.write(open(file_name, ‘w‘))
相关推荐
逍遥友 2020-11-20
pythonwangjunji 2020-08-16
hellowordmonkey 2020-11-02
gloria0 2020-10-26
zyshappy 2020-08-16
Equation 2020-08-09
iamjiyu 2020-07-21
winxcoder 2020-07-18
lepton 2020-07-04
zzdadexiaoha 2020-06-28
ajuan 2020-06-25
tosim 2020-06-24
wghou 2020-06-21
yudiewenyuan 2020-06-21
JessePinkmen 2020-06-14
heimu 2020-06-12
limx 2020-06-11
zghover 2020-06-11