.ini文件由若干section(部分)组成, 而每一个section又由若干键值对组成。

以 example.ini为例:

[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes [bitbucket.org]
User = hg [topsecret.server.com]
Port = 50022
ForwardX11 = no

创建.ini文件

import configparser

# configd对象类似于字典
config = configparser.ConfigParser()
config["DEFAULT"] = {
"ServerAliveInterval" : 45,
"Comperssion": "yes",
"ComperssionLevel": 9,
"ForwardX11": "yes"
} config["bitbucket.org"] = {}
config["bitbucket.org"]["user"] = "hg" config["topsecret.server.com"] = {}
config["topsecret.server.com"]["Port"] = ""
config["topsecret.server.com"]["ForwardX11"] = "no" with open("example.ini", "w") as fp:
# 写入文件
config.write(fp)

读取.ini文件

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

关于section

import configparser

config = configparser.ConfigParser()
config.read("example.ini")
# 获取section名称列表
print(config.sections()) # ['bitbucket.org', 'topsecret.server.com']
# 获取默认section名称
print(config.default_section) # DEFAULT
# 获取默认section对象
print(config.defaults()) # OrderedDict([('serveraliveinterval', '45'), ('comperssion', 'yes'), ('comperssionlevel', '9'), ('forwardx11', 'yes')]) # 判断是否包含对应的section
print('bitbucket.org' in config)
print(config.has_section("example.com")) # 添加section的2种方式
config['example.com'] = {}
config['example.com']['username'] = 'python'
config.add_section("example.com1") config.write(open("tmp.ini", "w")) # 删除section的2种方式
del config['example.com']
config.remove_section("example.com")
config.write(open("tmp.ini", "w"))

关于option

import configparser

config = configparser.ConfigParser()
config.read("example.ini") # 这里的option于key对应
# 获取section对应的key列表
bitbucket_keys = config.options('bitbucket.org')
print(bitbucket_keys) # ['user', 'serveraliveinterval', 'comperssion', 'comperssionlevel', 'forwardx11'] # 判断section中是否存在对应的key
print(config.has_option("bitbucket.org", "User"))
print('user1' in config.options('bitbucket.org'))
print('user1' in config['bitbucket.org']) # 添加option
config['bitbucket.org']['pwd'] = 'python'
config.write(open("tmp.ini", "w")) # 删除option的2种方式
del config['bitbucket.org']['User']
config.remove_option("bitbucket.org", "User")
config.write(open("tmp.ini", "w"))

value相关

import configparser

config = configparser.ConfigParser()
config.read("example.ini") # 获取key对应的value的2种方式
user = config['bitbucket.org']['User']
user = config.get('bitbucket.org', 'User')
print(user) # hg interval = config.getint("topsecret.server.com", "Port")
print(interval) #
compression = config.getboolean("topsecret.server.com", "ForwardX11")
print(compression)
# 还有 config.getfloat() # 设置value的2种方式
config.set('bitbucket.org', 'User', 'peter')
config['bitbucket.org']["User"] = "peter"
config.write(open("tmp.ini", "w"))

最新文章

  1. 《C专家编程》第二章——这不是Bug,而是语言特性
  2. Android应用程序窗口(Activity)的视图对象(View)的创建过程分析
  3. Apache(ApacheHaus)安装配置教程
  4. 【工具推荐】ELMAH——可插拔错误日志工具
  5. 【转】MySQL GRANT REVOKE用法
  6. BZOJ3856: Monster
  7. UML统一建模语言
  8. JSP九大内置对象和四个作用域
  9. vs2015安装没有wim32
  10. Ancient Message (古埃及象形文字识别 Uva 1103)
  11. 检查主机是否存活的shell脚本
  12. js控制TR的显示影藏
  13. Javascript实现Base64解码
  14. ios7对于NSString对象进行了的变更
  15. 【DFS】n皇后问题
  16. C#调用Microsoft.DirectX.DirectSound问题记录及解决
  17. HTTP的缓存策略
  18. mysql 更新(九) pymysql模块的使用
  19. HDU4791_Alice's Print Service
  20. vue + echarts画圈圈

热门文章

  1. helm安装 删除
  2. React文档(十七)非受控组件
  3. Oracle 11g streams部署
  4. Vue.js 3.0 新特性预览
  5. node.js面试题大全-侧重后端应用与对Node核心的理解
  6. linux服务管理 服务启动和自启动
  7. Faster-RCNN 自己的数据训练
  8. C# 语句中的各种单例模式代码
  9. python mysql and ORM
  10. python: super原理