configparser模块
此模块用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser。
来看一个好多软件的常见配置文件格式如下
[DEFAULT] 是每个section共有的
1 [DEFAULT] 2 serveraliveinterval = 45 3 compression = yes 4 compressionlevel = 9 5 forwardx11 = yes 6 7 [bitbucket.org] 8 user = hg 9 maxusers = 110 10 11 [topsecret.server.com] 12 port = 50022 13 forwardx11 = no 14 15 [group1] 16 name = lemi 17 age = 21 18 19 [group2] 20 name = kevin
解析配置文件
>>> import configparser # 导入模块 >>> config = configparser.ConfigParser() #实例化(生成对象) >>> config.sections() #调用sections方法 [] >>> config.read('example.ini') # 读配置文件(注意文件路径) ['example.ini'] >>> config.sections() #调用sections方法(默认不会读取default) ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config #判断元素是否在sections列表内 True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] # 通过字典的形式取值 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: print(key) # for循环 bitbucket.org 字典的key ... user compressionlevel serveraliveinterval compression forwardx11
增删改查语法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | import configparser conf = configparser.ConfigParser() conf.read( "conf.ini" ,encoding = 'utf-8' ) for k,v in conf[ 'bitbucket.org' ].items(): print (k,v) >>> user hg maxusers 110 serveraliveinterval 45 compression yes compressionlevel 9 forwardx11 yes print (conf.options( 'bitbucket.org' )) >>>[ 'user' , 'maxusers' , 'serveraliveinterval' , 'compression' , 'compressionlevel' , 'forwardx11' ] print (conf[ 'group1' ][ 'age' ]) >>> 21 print ( dir (conf)) >>> 'add_section' , 'clear' , 'converters' , 'default_section' , 'defaults' , 'get' , 'getboolean' , 'getfloat' , 'getint' , 'has_option' , 'has_section' , 'items' , 'keys' , 'options' , 'optionxform' , 'pop' , 'popitem' , 'read' , 'read_dict' , 'read_file' , 'read_string' , 'readfp' , 'remove_option' , 'remove_section' , 'sections' , 'set' , 'setdefault' , 'update' , 'values' , 'write' ] # 添加模块,并赋值 conf.add_section( 'group2' ) conf[ 'group2' ][ 'name' ] = 'kevin' conf[ 'group2' ][ 'age' ] = '22' conf.write( open ( 'conf.ini' , 'w' )) conf.add_section( 'group3' ) #赋值,修改 conf. set ( 'group3' , 'name' , 'fadewalk' ) conf.write( open ( 'conf.ini' , 'w' )) #删除section模块 conf.remove_section( 'group3' ) conf.write( open ( 'conf.ini' , 'w' )) #删除选项 conf.remove_option( 'group2' , 'age' ) conf.write( open ( 'conf.ini' , 'w' )) |
练习题
通过configparser模块完成以下功能 文件名my.cnf [DEFAULT] [client] port = 3306 socket = /data/mysql_3306/mysql.sock [mysqld] explicit_defaults_for_timestamp = true port = 3306 socket = /data/mysql_3306/mysql.sock back_log = 80 basedir = /usr/local/mysql tmpdir = /tmp datadir = /data/mysql_3306 default-time-zone = '+8:00'
修改时区 default-time-zone = '+8:00' 为 校准的全球时间 +00:00 删除 explicit_defaults_for_timestamp = true 为DEFAULT增加一条 character-set-server = utf8
import configparser config = configparser.ConfigParser() config.read('my.conf') config.set('mysqld','default-time-zone','+00:00') config.remove_option('mysqld','explicit_defaults_for_timestamp') config.set('DEFAULT','character-set-server','utf8') config.write(open('my.conf', "w")) print(config['mysqld']['default-time-zone'] )
本文来自博客园,作者:前海渔文乐,转载请注明原文链接:https://www.cnblogs.com/yuleicoder/p/py-configparser.html
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步