Python开发【内置模块篇】configparser
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python开发【内置模块篇】configparser,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2256字,纯文字阅读大概需要4分钟。
内容图文
![Python开发【内置模块篇】configparser](/upload/InfoBanner/zyjiaocheng/844/8b4ad5bdcfc3451bb3376e269fe7cada.jpg)
生成配置文件
![Python开发【内置模块篇】configparser - 文章图片](/upload/getfiles/0001/2021/5/6/20210506110704605.jpg)
![Python开发【内置模块篇】configparser - 文章图片](/upload/getfiles/0001/2021/5/6/20210506110704684.jpg)
import configparser config = configparser.ConfigParser() config["DEFAULT"] = {'ServerAliveInterval': '45', 'Compression': 'yes', 'CompressionLevel': '9', 'ForwardX11':'yes' } config['bitbucket.org'] = {'User':'hg'} config['topsecret.server.com'] = {'Host Port':'50022','ForwardX11':'no'} with open('example.ini', 'w') as f: config.write(f)View Code
![Python开发【内置模块篇】configparser - 文章图片](/upload/getfiles/0001/2021/5/6/20210506110704605.jpg)
![Python开发【内置模块篇】configparser - 文章图片](/upload/getfiles/0001/2021/5/6/20210506110704684.jpg)
[DEFAULT] serveraliveinterval = 45 compression = yes compressionlevel = 9 forwardx11 = yes [bitbucket.org] user = hg [topsecret.server.com] host port = 50022 forwardx11 = noexample.ini
普通操作
![Python开发【内置模块篇】configparser - 文章图片](/upload/getfiles/0001/2021/5/6/20210506110704605.jpg)
![Python开发【内置模块篇】configparser - 文章图片](/upload/getfiles/0001/2021/5/6/20210506110704684.jpg)
import configparser config = configparser.ConfigParser() # #---------------------------查找文件内容,基于字典的形式 print(config.sections()) # [] config.read('example.ini') # 读取文件指定文件 print(config.sections()) # ['bitbucket.org', 'topsecret.server.com'] print('bytebong.com' in config) # False print('bitbucket.org' in config) # True print(config['bitbucket.org']["user"]) # hg print(config['DEFAULT']['Compression']) #yes print(config['topsecret.server.com']['ForwardX11']) #no # print(config['bitbucket.org']) #<Section: bitbucket.org> for key in config['bitbucket.org']: # 注意,有default会默认读default的键 print(key) """ user serveraliveinterval compression compressionlevel forwardx11 """ print(config.options('bitbucket.org')) # 同for循环,找到'bitbucket.org'下所有键 # ['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11'] print(config.items('bitbucket.org')) #找到'bitbucket.org'下所有键值对 # [('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'hg')] print(config.get('bitbucket.org','compression')) # yes get方法Section下的key对应的value # yes获取配置信息
高级操作
![Python开发【内置模块篇】configparser - 文章图片](/upload/getfiles/0001/2021/5/6/20210506110704605.jpg)
![Python开发【内置模块篇】configparser - 文章图片](/upload/getfiles/0001/2021/5/6/20210506110704684.jpg)
import configparser config = configparser.ConfigParser() config.read('example.ini') # 读文件 config.add_section('yuan') # 增加section config.remove_section('bitbucket.org') # 删除一个section config.remove_option('topsecret.server.com',"forwardx11") # 删除一个配置项 config.set('topsecret.server.com','k1','11111') config.set('yuan','k2','22222') f = open('new2.ini', "w") config.write(f) # 写进文件 f.close() #这些操作不会修改example.ini的文件 因为是以读打开的文件读取example.ini写入new2.ini
![Python开发【内置模块篇】configparser - 文章图片](/upload/getfiles/0001/2021/5/6/20210506110704605.jpg)
![Python开发【内置模块篇】configparser - 文章图片](/upload/getfiles/0001/2021/5/6/20210506110704684.jpg)
[DEFAULT] serveraliveinterval = 45 compression = yes compressionlevel = 9 forwardx11 = yes [topsecret.server.com] host port = 50022 k1 = 11111 [yuan] k2 = 22222new2.ini
内容总结
以上是互联网集市为您收集整理的Python开发【内置模块篇】configparser全部内容,希望文章能够帮你解决Python开发【内置模块篇】configparser所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。