PyYAML模块

Python也可以很容易的处理ymal文档格式,只不过需要安装一个模块,参考文档:http://pyyaml.org/wiki/PyYAMLDocumentation

常用模块之ConfigParser模块

用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser。

来看一个好多软件的常见文档格式如下

[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'] = {}
config['bitbucket.org']['User'] = 'hg'
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)

写完了还可以再读出来哈。

 >>> import configparser
>>> config = configparser.ConfigParser()
>>> config.sections()
[]
>>> config.read('example.ini')
['example.ini']
>>> config.sections()
['bitbucket.org', 'topsecret.server.com']
>>> 'bitbucket.org' in config
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']
''
>>> for key in config['bitbucket.org']: print(key)
...
user
compressionlevel
serveraliveinterval
compression
forwardx11
>>> config['bitbucket.org']['ForwardX11']
'yes'

configparser增删改查语法

 [section1]
k1 = v1
k2:v2 [section2]
k1 = v1 import ConfigParser config = ConfigParser.ConfigParser()
config.read('i.cfg') # ########## 读 ##########
#secs = config.sections()
#print secs
#options = config.options('group2')
#print options #item_list = config.items('group2')
#print item_list #val = config.get('group1','key')
#val = config.getint('group1','key') # ########## 改写 ##########
#sec = config.remove_section('group1')
#config.write(open('i.cfg', "w")) #sec = config.has_section('wupeiqi')
#sec = config.add_section('wupeiqi')
#config.write(open('i.cfg', "w")) #config.set('group2','k1',11111)
#config.write(open('i.cfg', "w")) #config.remove_option('group2','age')
#config.write(open('i.cfg', "w"))

最新文章

  1. Android指纹识别深入浅出分析到实战(6.0以下系统适配方案)
  2. 那些让IE6-8羞愧的替补型js
  3. JVM内存模型、指令重排、内存屏障概念解析
  4. VBA常用函数
  5. windows IIS6 PHP搭建
  6. bzoj2565: 最长双回文串
  7. Sie sind das Essen und wir sind die Jaeger!
  8. C++实现的屏幕截图软件 v1.0
  9. always语言指导原则
  10. C++编程技巧(长期更新)
  11. python编写文件统计脚本
  12. Maven项目下servlet异常
  13. BZOJ1024:[SCOI2009]生日快乐(DFS)
  14. LAMP架构之PHP-FPM 服务器
  15. 38 Cell-phone Emissions can change Brain Activity 手机辐射有可能改变大脑活动
  16. ImportError: cannot import name 'izip & TypeError: 'float' object cannot be interpreted as an integer
  17. Vue笔记之模板语法
  18. splash启动速度优化
  19. 初识jQuery(2)
  20. db2 v9.5迁移至v10.5,及遇重名节点数据库无法创建db的解决办法

热门文章

  1. 序列化和反序列化的几种方式(DataContractSerializer)
  2. iOS多线程的初步研究3
  3. SpringMVC,Spring,Hibernate,Mybatis架构开发搭建之SpringMVC部分
  4. Centos6.3不能使用yum install安装gcc编辑器解决办法
  5. mac系统及xcode使用的SVN客户端升级
  6. Tapestry5.3使用总结
  7. 【deep learning学习笔记】注释yusugomori的RBM代码 --- 头文件
  8. Speex Acoustic Echo Cancellation (AEC) 回声消除模块的使用
  9. C++ 头文件系列(array)
  10. 关于SQL 数据表中的密码加密