configparse模块

1.生成文件

import configparser        # 配置解析模块

config = configparser.ConfigParser()       # config = {  }

config['default_1'] = {'a':'',                   # 第一种写入的方法
'b':'',
'c':''
} config['default_2'] = {} # 第二种方法
config['default_2']['user'] = 'abc' config['default_3'] ={}
li = config['default_3']
li['passwd'] = ''
li['addres'] = 'none' with open('test_config','w') as f:
config.write(f)

运行之后 test_config文件里的内容:

[default_1]
a = 1
b = 2
c = 3 [default_2]
user = abc [default_3]
passwd = 456789
addres = none

2.对文件的操作

import configparser

config = configparser.ConfigParser()

# 查询

config.read('test_config')
print(config.sections())
print('default_3' in config)
print(config['default_3']['passwd']) for key in config['default_1']:
print(key) print(config.options('default_3'))
print(config.items('default_3'))
print(config.get('default_3','passwd'))
import configparser

config = configparser.ConfigParser()

config.read('test_config')
# 增,删,改 config.add_section('default_4')
config.set('default_4','user','john') config.remove_section('default_2')
config.remove_option('default_3','passwd') config.write(open('test_config_2','w'))

python学习-58 configparse模块的更多相关文章

  1. Python学习day19-常用模块之re模块

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  2. Python学习 Part4:模块

    Python学习 Part4:模块 1. 模块是将定义保存在一个文件中的方法,然后在脚本中或解释器的交互实例中使用.模块中的定义可以被导入到其他模块或者main模块. 模块就是一个包含Python定义 ...

  3. python学习之argparse模块

    python学习之argparse模块 一.简介: argparse是python用于解析命令行参数和选项的标准模块,用于代替已经过时的optparse模块.argparse模块的作用是用于解析命令行 ...

  4. Python学习day18-常用模块之NumPy

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  5. python学习之random模块

    Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...

  6. Python学习笔记之模块与包

    一.模块 1.模块的概念 模块这一概念很大程度上是为了解决代码的可重用性而出现的,其实这一概念并没有多复杂,简单来说不过是一个后缀为 .py 的 Python 文件而已 例如,我在某个工作中经常需要打 ...

  7. Python学习笔记-常用模块

    1.python模块 如果你退出 Python 解释器并重新进入,你做的任何定义(变量和方法)都会丢失.因此,如果你想要编写一些更大的程序,为准备解释器输入使用一个文本编辑器会更好,并以那个文件替代作 ...

  8. Python学习笔记1—模块

    模块的使用 引用模块的两种形式 形式一: import module_name 形式二: from module1 import module11   (module11是module的子模块) 例: ...

  9. Python学习笔记2——模块的发布

    1.为模块nester创建文件夹nester,其中包含:nester.py(模块文件): """这是"nester.py"模块,提供了一个名为prin ...

随机推荐

  1. 转 python多个命令同时执行.sh

    1.背景是 有三个脚本a.py, b.py, c.py 三个都是爬虫,里面都是while(true)方式运行的,不会主动运行结束. 每次启动他们,就需要: python a.py > logs/ ...

  2. linux xlearn安装

     机器学习中的又一个利器,广泛用于Kaggle或类似的数据比赛. xlearn的优势: 1.通用性好,包括主流的算法(lr, fm, ffm 等),用户不用再切换于不同软件之间 2.性能好,测试 xL ...

  3. Ubuntu --- 安装deb包

    在Ubuntu下安装deb包需要使用dpkg命令.Dpkg 的普通用法: 1.sudo dpkg -i <package.deb> 安装一个 Debian 软件包,如你手动下载的文件. 2 ...

  4. comparison of truncate vs delete in mysql/sqlserver

    comparison of truncate vs delete in mysql/sqlserver [duplicate]   DELETE DELETE is a DML Command. DE ...

  5. flutter PopupMenuButton弹出式菜单列表

    import 'package:flutter/material.dart'; class PopupMenuButtonDemo extends StatefulWidget { @override ...

  6. memcpy字节序问题

    /* memcpy用法详解 */ #include <stdio.h> #include <stdlib.h> #include <string.h> //memc ...

  7. 004-行为型-03-观察者模式(Observer)

    一.概述 当对象间存在一对多关系时,则使用观察者模式(Observer Pattern).比如,当一个对象被修改时,则会自动通知它的依赖对象. 定义了对象之间的一对多依赖,让多个观察者对象同时监听某一 ...

  8. Qt QLabel加载图片

    QLabel加载图片 //在对应的控件中显示图片 void qm_img::DisplayImg(cv::Mat imgParam, QLabel *labelParam) { if (!imgPar ...

  9. matlab @(x)构造匿名函数

    一起来学演化计算-matlab@(x)构造匿名函数 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 https://www.ilovematlab.cn/thread-81614-1 ...

  10. linux下配置face_recognition

    1.如linux下已有python2.7,但需要更新一下python 2.7至python2.x sudo add-apt-repository ppa:fkrull/deadsnakes-pytho ...