yaml模块:

python可以处理yaml文件,yaml文件安装的方法为:$ pip3 install pyyaml

   configparser模块,用来处理文件的模块,可以实现文件的增删改查

configparser用于处理特定格式的文件,其本质上是利用open来操作文件

下面来看看configarser模块的功能:

[DEFAULT]
serveraliveinterval =
compression = yes
compressionlevel =
forwardx11 = yes [bitbucket.org]
user = hg [topsecret.server.com]
host port =
forwardx11 = no

上面代码格式就是configparser的常见格式,这中文件格式在有些地方很常见。

import configparser

config = configparser.ConfigParser()
config["DEFAULT"] = {"ServerAliveInterval":"",
"compression":"yes",
"CompressionLevel":""} config["bitbucket.org"] = {}
config["bitbucket.org"]["user"] = "hg"
config["topsecret.server.com"] = {} #定义一个空的字典
topsecret = config["topsecret.server.com"] #把空的字典赋值给topsecret,生成一个空字典
topsecret["Host Port"] = "" #给字典添加键值对
topsecret["Forwardx11"] = "no"
config["DEFAULT"]["Forwardx11"] = "yes"
with open("config_file.ini","w") as configfile:
config.write(configfile) 运行如下:
[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes [bitbucket.org]
user = hg [topsecret.server.com]
host port = 50022
forwardx11 = no

从上面代码可以看出,其文件的格式类似于字典的形式,包含keys和values类型,我们首先要定义一个configparser的文件,然后往文件中添加键值对。如上面代码所示:

上面我们把字典写进文件之后,如何读取呢?下面来看看configparser.ConfigParser的文件操作:

    读取:

>>> import configparser
  >>> config = configparser.ConfigParser()           #定义一个文件信息
  >>> config.sections()
  []

  >>> config.read('example.ini')
  ['example.ini']
   
    >>> config.sections()
    ['bitbucket.org', 'topsecret.server.com']
    下面来看看configparser模块中文件的增删改查:
import configparser

config = configparser.ConfigParser()
config.read("config_file.ini") #删除文件中的字段
sec = config.remove_section("bitbucket.org")
config.write(open("config_file.ini","w")) #添加新的字段到文件中
# sec = config.has_section("alex")
# config.add_section("alex")
# config["alex"]["age"] = ""
# config.write(open("config_file.ini","w")) #修改字段中的文件信息
config.set("alex","age","")
config.write(open("config_file.ini","w"))

上面代码的字段中,我们实现了增删改查的功能。要了解文件中的功能即可。并且能够操作,其实很多时候都对文件操作的思路都是相同的,只是实现的方式不一样,代码的写法不一样而已。

day6 ConfigParser模块 yaml模块的更多相关文章

  1. Day6 Python常用的模块

    一.logging模块 一.日志级别 critical=50 error=40 waring=30 info=20 debug=10 notset=0 二.默认的日志级别是waring(30),默认的 ...

  2. pytho day6 <正则表达式、常用模块、反射>

    本节介绍: 一:正则表达式: 正则表达并不是python 独有的.在各个语言里都有该语法的介绍.正则表达是处理字符串的强大的处理工具.拥有自己的独特的 处理方法.和处理引擎.虽然性能没有python ...

  3. Python(文件、文件夹压缩处理模块,shelve持久化模块,xml处理模块、ConfigParser文档配置模块、hashlib加密模块,subprocess系统交互模块 log模块)

    OS模块 提供对操作系统进行调用的接口 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname")  改变当前脚本工作目 ...

  4. s14 第5天 时间模块 随机模块 String模块 shutil模块(文件操作) 文件压缩(zipfile和tarfile)shelve模块 XML模块 ConfigParser配置文件操作模块 hashlib散列模块 Subprocess模块(调用shell) logging模块 正则表达式模块 r字符串和转译

    时间模块 time datatime time.clock(2.7) time.process_time(3.3) 测量处理器运算时间,不包括sleep时间 time.altzone 返回与UTC时间 ...

  5. python之hashlib、configparser、logging模块

    hashlib模块 Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 什么是摘要算法呢?摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定的数 ...

  6. hashlib模块configparser模块logging模块

    hashlib模块 算法介绍 Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 什么是摘要算法呢?摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长 ...

  7. os模块,os.path模块,subprocess模块,configparser模块,shutil模块

    1.os模块 os表示操作系统该模块主要用来处理与操作系统相关的操作最常用的文件操作打开 读入 写入 删除 复制 重命名 os.getcwd() 获取当前执行文件所在的文件夹路径os.chdir(&q ...

  8. configparser、subprocess模块

    一.configparser模块 该模块适用于配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键=值). 1.创建文件 一般软件的常见文档 ...

  9. python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess logging re正则

    python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib  subprocess ...

随机推荐

  1. ElasticStack系列之八 & _source 字段

    有很多人会有这样的一个疑问: _source字段存储的是索引的原始内容,那 store 属性的设置是为何呢?elasticsearch 为什么要把 store 的默认取值设置为 no?设置为 yes ...

  2. 2018年9月22日CCPC吉林站参赛总结

    发现思维题是硬伤,代码能力是硬伤,对知识点的理解不深刻是硬伤 接下来要做的就是 1.熟悉每一个知识点,把每一个知识点和实现它的代码联系在一起学习 2.多见题,看看他们是怎么考察这些知识点的,等比赛的时 ...

  3. dp px 互转工具类

    public class DensityUtils { public static int dpToPx(Context context,int dp){ float density = contex ...

  4. C++ Core Guidelines

    C++ Core Guidelines September 9, 2015 Editors: Bjarne Stroustrup Herb Sutter This document is a very ...

  5. spring框架学习(五)整合JDBCTemplate

    1.导包 2.JdbcTemplate package cn.cnki.JdbcTemplate; import java.util.List; import org.junit.Test; impo ...

  6. 2016-2017-20155329 《Java程序设计》第7周学习总结

    学号 2016-2017-20155329 <Java程序设计>第7周学习总结 教材学习内容总结 时间的度量 格林威治标准时间(GMT时间) 世界时(UT) 国际原子时(TAI) 世界协调 ...

  7. Trying to get property of non-object

    原文:http://www.jb51.net/article/29878.htm 总结:判断为空用 if(isset($v)){}代替if($v == null){}

  8. 七牛云 上传图片 https 修改Nginx 注意事项

    仅在这记录下,今天的事情. 问题出自于Nginx 设置http 强制跳转 https设置 1.上午,出于某些需求,我将服务器Nginx 设置http 强行跳转 https server { liste ...

  9. URAL 1416 Confidential (最小生成树+次小生成树)

    Description Zaphod Beeblebrox - President of the Imperial Galactic Government. And by chance he is a ...

  10. Python练习-从小就背不下来的99乘法表

    心血来潮,灵机一动,反正就是无聊的做了一个很简单的小玩意: for i in range(1,10):#让i 1-9 循环9次 print("\n")#每循环一次进行一次换行 fo ...