首页 > 其他 > 详细

pythopn configparser 模块(配置)

时间:2018-01-13 22:10:45      阅读:233      评论:0      收藏:0      [点我收藏+]
ConfigParser(py2)模块在python3中修改为configparser该类的作用是使用配置文件生效, 配置文件的格式和windows的INI文件的格式相同 该模块的作用 就是使用模块中的RawConfigParser()、ConfigParser()、 SafeConfigParser() 这三个方法(三者择其一),创建一个对象使用对象的方法对指定的配置文件做增删改查 操作。 配置文件有不同的片段组成和Linux中repo文件中的格式类似: [section] name=value 或者 name: value "#" 和";" 表示注释 [DEFAULT] ServerAliveInterval = 45 Compression = yes CompressionLevel = 9 ForwardX11 = yes [bitbucket.org] User = hg [topsecret.server.com] Port = 50022 ForwardX11 = no 如果想用python生成一个这样的文档怎么做呢? >>> import configparser >>> config = configparser.ConfigParser() >>> config["DEFAULT"] = {‘ServerAliveInterval‘: ‘45‘, >>> ‘Compression‘: ‘yes‘, >>> ‘CompressionLevel‘: ‘9‘} >>> config[‘bitbucket.org‘]={‘User‘:‘lb‘} >>> config[‘topsecret.server.com‘] = {} >>> topsecret = config[‘topsecret.server.com‘] >>> topsecret[‘Host Port‘] = ‘50022‘ # mutates the parser >>> topsecret[‘ForwardX11‘] = ‘no‘ # same here >>> config[‘DEFAULT‘][‘ForwardX11‘] = ‘yes‘ >>> with open(‘example.ini‘, ‘w‘) as configfile: >>> config.write(configfile)

python的ConfigParser

pythopn configparser 模块(配置)

原文:http://blog.51cto.com/13562606/2060669

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!