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

Python configparser模块

时间:2022-11-03 11:23:56浏览次数:35  
标签:key 配置文件 Python section mysqld ini 模块 config configparser

1、configparser 模块介绍:一般做自动化测试的时候,会使用到这个模块,用来操作配置文件(ini文件)封装一些常量。比如数据库、邮件、用户名密码、项目常量等等

2、ini 文件是一种常用配置文件,ini 文件主要如下:

  • ini 文件格式,由节、键、值组成
    • [section]   # 节  
    • key = value   # key:键,value:值
      • 一个配置文件中可以有多个 section,每个 section 名字不能相同,每个 section 下可以分多个键值,每个 section 下的键不能相同;例如:MySQL 配置文件格式为 ini 文件,部分内容如下:
[mysqld_safe]
socket = /var/run/mysqld/mysqld.sock
nice = 0

[mysqld]
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
bind-address = 127.0.0.1
key_buffer_size = 16M
log_error = /var/log/mysql/error.log
  •  ini 文件创建
    • 在 Pycharm 编辑器中,如果右击 new scratch file 中没有 ini 文件选项,则需要安装插件,步骤如下
      • Pycharm 点击 File 选择 setting 选项    
      • 进入 Plugins 在 Marketplace 中找到 Ini 插件点击安装,然后重启 Pycharm  
      • 再次进入 new scratch file 中就能找到 ini 文件  
  •  ini 文件读取操作,ini 文件需要通过 configparser 模块操作,configparser 是 Python 中自带模块,方法操作如下
    • config = configparser.ConfigParser()    创建 ConfigParser 对象  
    • config.read(filenames, encoding=None)   读取配置文件  
    • config.sections()   获取所有的 section  
    • config.options(section)   获取指定 section 下所有的 key  
    • config.get(section, option,…)   获取指定 section 下指定 key 的值  
    • config.items(section,…)   获取指定 section 下所有 key 与 value  
# 以上面的的配置文件数据为例

import configparser
#配置文件路径:
path = "C:/Users\Administrator\.PyCharmCE2019.1\config\scratches\scratch.ini"
#创建ConfigParser模块
config = configparser.ConfigParser()
#读取文件
config.read(path, encoding="utf-8")
#获取所有的section
sections = config.sections()
print(sections)
#获取section为mysqld_safe下所有key
keys = config.options("mysqld_safe")
print(keys)
#获取section为mysqld_safe下所有的key-value
items = config.items("mysqld_safe")
print(items)
#获取section为mysqld,key为log_error对应的value
val = config.get("mysqld", "log_error")
print(val)

# 结果如下
['mysqld_safe', 'mysqld']
['socket', 'nice']
[('socket', '/var/run/mysqld/mysqld.sock'), ('nice', '0')]
/var/log/mysql/error.log
  •  ini 文件修改操作
    • config.set(section, option, value=None) 设置section中指定key的value值,key不存在,直接添加key-value
    • config.add_section(section) 配置信息中添加section
    • config.remove_section(section) 删除section
    • config.remove_option 删除指定section中key
    • config.write(fp, space_around_delimiters=True) 将配置信息写回到配置文件
  • 检查 section 与 key 的方法:
    • config.has_section(section) 检查指定section是否存在
    • config.has_option(section, option) 检查指定section下option是否存在
import configparser
#配置文件路径
path = "C:/Users\Administrator\.PyCharmCE2019.1\config\scratches\scratch.ini"
#新配置文件
newpath = "C:/Users\Administrator\.PyCharmCE2019.1\config\scratches\scratch1.ini"
#读取配置文件
config = configparser.ConfigParser()
config.read(path)
#是否存在section:mysqld
if not config.has_section("mysqld"):
    config.add_section("mysqld")
#设置logerror路径
    config.set("mysqld", "log_error", "D:\Evan_duoceshi\my_code_file\practice\configparser_module/mysql_error.log")
#添加section:Mysqldump
if not config.has_section("mysqldump"):
    config.add_section("mysqldump")
    config.set("mysqld", "max_allowed_packet", "16M")
    #打开要写入新的配置文件路径
    wf = open(newpath, "w")
    #写入文件
    config.write(wf, space_around_delimiters=True)
    wf.close()

 

标签:key,配置文件,Python,section,mysqld,ini,模块,config,configparser
From: https://www.cnblogs.com/ZhengYing0813/p/16853822.html

相关文章

  • Python实验报告(第八周)
    一、实验目的1.认识程序运行中的异常2.学会处理异常语句3.学会调试程序二、实验环境python版本:3.10(64-bit)三、实验内容1.实例一:  实验结果:  2.实例二:  ......
  • 力扣1668(java&python)-最大重复子字符串(简单)
    题目:给你一个字符串 sequence ,如果字符串word 连续重复 k 次形成的字符串是 sequence 的一个子字符串,那么单词 word的重复值为k。单词word 的最大重复值......
  • Python多进程
    title:python多进程date:2022-10-2718:51:33categories:-教程tags:-python大家看过前面文章的应该都知道python中的GIL的存在,也就是多线程的时候,同一时间......
  • Python+requests获取重定向的url地址
       importrequestsurl='http://1234by.com'res=requests.get(url,headers={"Content-Type":"application/json"})redit_list=res.history#可以看出获取......
  • python汉字转拼音,中英文单词互译,中英文简单句子翻译
    由于在学java,需要定义变量,拼音不好,英文也差,定义变量成了头痛的事,所以写这个脚本#!/usr/bin/python3#pip升级不了时可以python3get-pip.py#需要安装pinyin包pi......
  • 盘点一个使用Python实现Excel数理统计的实战问题
    大家好,我是皮皮。一、前言前几天在小小明大佬的Python交流群中遇到一个粉丝问了一个使用Python实现Excel数理统计的实战问题,觉得还挺有用的,这里拿出来跟大家一起分享下。......
  • tensorflow1.x——如何在python多线程中调用同一个session会话
    如何在python多线程中调用同一个session会话? 这个问题源于我在看的一个强化学习代码:https://gitee.com/devilmaycry812839668/scalable_agent 在众多的机器学习的分......
  • Python第九章实验报告
    一、实验题目Python第九章实例二、实验目的和要求1.熟悉Pycharm的运行环境2.学习并掌握Python的异常处理及程序调试三、主要仪器设备联想小新air15硬件:AMDR75700U......
  • Liunx安装python3环境
    Linux服务器安装python3环境Linux服务器默认是2.7.5,但在使用过程中可能需要python3环境,本文介绍如何安装python31、安装python依赖包,主要是gcc编译环境,zlib解压缩包等y......
  • python中的列表和元组
     #1.列表list#列表可以存储多个不同类型的数据print('1.列表')#定义一个列表最好存储同一类的数据,这样操作方便list1=[]#定义一个空列表name_list=['......