day⑥:configparser模块

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

一.生成一个configpaser文档

  1. #!/usr/bin/env python
  2. #coding=utf-8
  3. __author__ = ‘yaobin‘
  4. #生成一个configparse文档
  5. import configparser
  6. config = configparser.ConfigParser()
  7. #生成第一个节段
  8. config["DEFAULT"] = {‘ServerAliveInterval‘: ‘45‘,
  9. ‘Compression‘: ‘yes‘,
  10. ‘CompressionLevel‘: ‘9‘}
  11. #生成第二个节段
  12. config[‘bitbucket.org‘] = {}
  13. config[‘bitbucket.org‘][‘User‘] = ‘hg‘
  14. config[‘bitbucket.org‘][‘passwd‘] = ‘123456‘
  15. #生成第三个节段
  16. config[‘topsecret.server.com‘] = {}
  17. topsecret = config[‘topsecret.server.com‘]
  18. topsecret[‘Host Port‘] = ‘50022‘ # mutates the parser
  19. topsecret[‘ForwardX11‘] = ‘no‘ # same here
  20. config[‘DEFAULT‘][‘ForwardX11‘] = ‘yes‘
  21. with open(‘example.ini‘, ‘w‘) as configfile:
  22. config.write(configfile)

二.读增删改

  1. #!/usr/bin/env python
  2. #coding=utf-8
  3. __author__ = ‘yaobin‘
  4. import configparser
  5. config = configparser.ConfigParser()
  6. config.read(‘example.ini‘)
  7. # ########## 读 ##########
  8. # secs = config.sections()
  9. # print(secs)
  10. # options = config.options(‘bitbucket.org‘)
  11. # print(options)
  12. # item_list = config.items(‘bitbucket.org‘)
  13. # print(item_list)
  14. # val = config.get(‘bitbucket.org‘,‘user‘)
  15. # val2 = config.getint(‘bitbucket.org‘,‘passwd‘)
  16. # print(val)
  17. # print(val2)
  18. # a="bitbucket.org" in config
  19. # print(a)
  20. #
  21. # b="bitbucket.orgno" in config
  22. # print(b)
  23. # d=config[‘bitbucket.org‘][‘User‘]
  24. # e=config[‘DEFAULT‘][‘Compression‘]
  25. # print(d)
  26. # print(e)
  27. # topsercret=config[‘topsecret.server.com‘]
  28. # print(topsercret[‘forwardx11‘])
  29. # print(topsercret[‘host port‘])
  30. # for key in config[‘bitbucket.org‘]:
  31. # print(key) #DEFAULT也打印出来了,DEFAULT是全局变量吧
  32. #print(config[‘bitbucket.org‘][‘ForwardX11‘]) #打印默认的出来了
  33. # ########## 改写 ##########
  34. # sec = config.remove_section(‘bitbucket.org‘)
  35. # config.write(open(‘a.cfg‘, "w"))
  36. # sec = config.has_section(‘bitbucket.org‘)
  37. # print(sec)
  38. # config.add_section(‘yaobin‘)
  39. # config.write(open(‘b.cfg‘, "w"))
  40. #
  41. # config.set(‘yaobin‘,‘k1‘,"11111")
  42. # config.write(open(‘c.cfg‘, "w"))
  43. #
  44. # config.remove_option(‘bitbucket.org‘,‘passwd‘)
  45. # config.write(open(‘d.cfg‘, "w"))

来自为知笔记(Wiz)

时间: 2024-08-04 17:59:35

day⑥:configparser模块的相关文章

python学习-shutil,configparser模块

shutil模块 shutil模块提供了大量的文件的高级操作.特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作.对单个文件的操作也可参见os模块. shutil.copyfileobj(fsrc, fdst[, length]) shutil.copyfileobj(fsrc, fdst[, length]):复制文件内容(不包含元数据)从类文件对象src到类文件对dst.可选参数length指定缓冲区的大小,负数表示一次性读入.默认会把数据切分成小块拷贝,以免占用太多内存.注意:拷

Python中ConfigParser模块应用

Python中ConfigParser模块应用 Python的ConfigParser模块定义了3个对INI文件进行操作的类 RawConfigParser,ConfigParser和SafeConfigParser.其中RawCnfigParser是最基础的INI文件读取类,ConfigParser.SafeConfigParser支持对%(value)s变量的解析. 下面看看怎样通过ConfigParser类来解析一个ini文件. 配置文件settings.cfg [DEFAULT] myk

day6 ConfigParser模块 yaml模块

    yaml模块: python可以处理yaml文件,yaml文件安装的方法为:$ pip3 install pyyaml    configparser模块,用来处理文件的模块,可以实现文件的增删改查 configparser用于处理特定格式的文件,其本质上是利用open来操作文件 下面来看看configarser模块的功能: [DEFAULT] serveraliveinterval = 45 compression = yes compressionlevel = 9 forwardx

python 之ConfigParser模块学习

1.1 读取配置文件 -read(filename) 直接读取ini文件内容 -sections() 得到所有的section,并以列表的形式返回 -options(section) 得到该section的所有option -items(section) 得到该section的所有键值对 -get(section,option) 得到section中option的值,返回为string类型 -getint(section,option) 得到section中option的值,返回为int类型 1

python学习笔记-Day7(configparser模块、shutil、压缩与解压模块、subprocess)

configparser模块 # configparser用于处理特定格式的文件,其本质上是利用open来操作文件 # 下边我们就创建这种特定格式配置文件,来操作以下这里模块方法 --------------test.conf---------------- [section1] # configparser 会认定以中括号括住的为一个节点(node) k1 = 111 # 节点下,每一行配置文件为键值对存在(也可以写成 k2:123) k2 = v2 k3 = 123 k4 = True k1

ConfigParser模块

ConfigParser模块记录常用方法 #!/usr/bin/env python #coding: utf-8 import ConfigParser def main():     """"     基本的读取配置文件     -read(filename) 直接读取ini文件内容     -sections() 得到所有的section,并以列表的形式返回     -options(section) 得到该section的所有option     -item

Python学习笔记——基础篇【第六周】——PyYAML & configparser模块

PyYAML模块 Python也可以很容易的处理ymal文档格式,只不过需要安装一个模块,参考文档:http://pyyaml.org/wiki/PyYAMLDocumentation 常用模块之ConfigParser模块 用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 来看一个好多软件的常见文档格式如下 [DEFAULT] ServerAliveInterval = 45 Compression = yes Compression

Python ConfigParser模块常用方法示例

 在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在Python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是ConfigParser,这里简单的做一些介绍.      Python ConfigParser模块解析的配置文件的格式比较象ini的配置文件格式,就是文件中由多个section构成,每个section下又有多个配置项,比如:      [db]     db_host=192.168.1.1    db_port=3306    db_

python ConfigParser模块 配置文件解析

ConfigParser模块主要是用来解析配置文件的模块,像mysql,或者win下面的ini文件等等 下面我们来解析mysql的配置文件my.cnf my.cnf配置文件内容 [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic

Python开发基础-Day15正则表达式爬虫应用,configparser模块和subprocess模块

正则表达式爬虫应用(校花网) 1 import requests 2 import re 3 import json 4 #定义函数返回网页的字符串信息 5 def getPage_str(url): 6 page_string=requests.get(url) 7 return page_string.text 8 9 hua_dic={} 10 def run_re(url): #爬取名字.学校和喜爱的人数 11 hua_str=getPage_str(url) 12 hua_list=r