首页 > 编程语言 >Python 之configparser模块

Python 之configparser模块

时间:2022-10-17 09:11:26浏览次数:45  
标签:选项 option Python section 模块 file test config configparser

一、示例

'''
添加
add_section(section)    向实例添加一个section
set(section, option, value)    如果给定的部分存在,将给定的选项设置为指定的值
optionxform(option)    也可以在一个实例上重新设置它,对于一个需要字符串参数的函数。例如,将其设置为str,将使选项名称区分大小写

查找
defaults()    返回包含实例范围默认值的字典。
sections()    返回可用的section的列表;默认section不包括在列表中
has_section(section)    指示指定的section是否出现在配置中。默认的section未被确认
options(section)    返回指定section中可用的选项列表。
has_option(section, option)    如果给定的section存在,并且包含给定的选项,则返回True;否则返回False
get(section, option)    为指定的section获取一个选项值。
getint(section, option)    它将指定section中的选项强制转换为整数
getfloat(section, option)    它将指定section中的选项强制转换为浮点型
getboolean(section, option)    强制转换为布尔型,”1”, “yes”, “true”, and “on”, 转换为True,”0”, “no”, “false”, and “off”, 转换为Falseo 其他返回ValueError.
items(section)    返回给定section中每个选项的(name,value)对的列表。

删除
remove_option(section, option)  从指定的部分中删除指定的选项。如果该部分不存在,请提出NoSectionError。如果存在的选项被删除,返回True;否则返回False。
remove_section(section) 从配置中删除指定的section。如果这个部分确实存在,返回True。否则返回假

判断是否存在
has_option(section, option)
has_section(section)
'''

import configparser

if __name__ == '__main__':
    config_file = configparser.ConfigParser()

    # 创建配置文件
    config_file.add_section("test")
    # 添加
    config_file.set("test", "username", "yy")
    config_file.set("test", "password", "123")
    config_file.set("test", "salt", "abc")
    #修改
    config_file["test"].update({"username": "yy1015"})
    #删除
    config_file.remove_option("test", "salt")
    with open("config.ini","w") as file_object:
        config_file.write(file_object)
    
    # 读取配置文件
    config_file.read('config.ini')
    print(config_file.get("test", "username"))
    print(config_file.has_option("test", "salt"))
    

 

标签:选项,option,Python,section,模块,file,test,config,configparser
From: https://www.cnblogs.com/yang-2018/p/16797925.html

相关文章

  • python安装与python、pip的环境变量配置
    进入官网在你常用的搜索引擎中输入python官网然后进入。可直接点击本链接python官网进入;也可在浏览器地址栏输入www.python.org回车进入官网。下载将鼠标放到菜......
  • 本地线上运行python代码
    http://localhost:39093/1、learning.py文件的代码#!/usr/bin/envpython3#-*-coding:utf-8-*-r'''learning.pyAPython3tutorialfromhttp://www.liaox......
  • [oeasy]python0007-调试程序_debug
    ​ 调试程序......
  • 盘点一个Python自动化办公的实战案例
    大家好,我是皮皮。一、前言前几天在Python钻石交流群【Hxy任我肥】问了一个Python自动化办公的问题,提问截图如下:想要的效果是下图这样的:准确来说,这个都不算是问题了,而......
  • python系列13:python中Path常用功能
     1.基本功能 建议使用pathlib模块来处理文件和文件夹,可以跨平台。pathlib提供path对象来操作,包括目录和文件。In[1]:frompathlibimportPathIn[2]:p=Path()In......
  • python学习第三周总结
    文件操作文件的读写模式文件的操作模式文件相关操作文件内光标移动文件内容修改函数前戏函数的语法结构函数的定义和调用函数的分类函数......
  • ROS2基本命令与简单列子(python与C++)
    初次学习ROS2机器操作系统,本博客将简单入门记录于此。 一. ros2安装:sudoapt-getinstallcurl&&curl http://fishros.com/tools/install/ros-foxy|bash二.编......
  • python学习——爬取数据到mysql
    承接上文,上次把数据爬取到了excel中,这次在上次代码的基础上进行修改,将数据直接上传到mysql中#-*-coding:utf-8-*-importrequestsfrombs4importBeautifulSoupim......
  • 65、记录使用科大讯飞的声纹识别从官方的Python Demo转C++ Demo路程
    基本思想:需要将声纹识别的demo集成到项目中,奈何官方只提供了py版本和java版本,需要c++版本,逐开发和记录一下,只是简单复现其py代码一、官方代码的和手册的地址 这里将py代码......
  • 【Python】Centos7安装Python3和pip
    安装Python3#wgethttps://www.python.org/ftp/python/3.6.2/Python-3.6.2.tar.xz#tar-xvJfPython-3.6.2.tar.xz#cdPython-3.6.2#./configure--prefix=/data......