当前位置: 首页 > article >正文

Python-封装配置文件

在这里插入图片描述

在这里插入图片描述

Code

[url]
baidu = http://www.baidu.com

[value]
send_value = 百度

[server]
ip = 220.181.111.188

封装的格式可以套用

# 封装,类似函数调用
import configparser

class ReadConfigIni():

    def __init__(self,filename):
        self.cf = configparser.ConfigParser()
        self.cf.read(filename)

    def getConfigValue(self,section,name):
        value = self.cf.get(section,name)
        # print(value)
        return value

DoConfigIni = ReadConfigIni("test.ini")  #路径+文件名
BaiUrl = DoConfigIni.getConfigValue("url","baidu")
print("通过函数getConfigValue获得的值:",BaiUrl)

cf.ini文件

[db]
db_host = 127.0.0.1
db_port = 80
db_user = root
db_pass = root
host_port = 69

[concurrent]
thread = 10
processor = 20
kong = 


op_cf.py文件

ini文件的增删查找演示

操作完成后都要加上 config.write(open(“cf.ini”,“w”)) (易错点)

# author:xiaowang
# date:2023/12/6
# 需求:ini文件的增删查找演示 操作完成后都要加上 config.write(open("cf.ini","w"))  #写入ini配置文件(易错点)
# 操作文档py
import configparser

config = configparser.ConfigParser()   #实例化配置文件
config.read("cf.ini",encoding="utf-8")  #读取配置文件

# 查找/读取操作
print(config.sections()) #读取所有的节
print(config.options("db"))  #指定节中键
print(config.get("db", "db_host")) #指定db节中db_host键中的值
print(config.get("concurrent","processor")) #指定节中的指定键的值
print(config.get("concurrent","kong"))
print(config.items("db"))  #输出指定db节的所有键值对(k:v)


# 修改操作并通过读取验证
config.set("db","db_port","80") #将原来db_port的值69修改为80
config.write(open("cf.ini","w"))
print(config.get("db","db_port"))

# 检查section 或 参数是否存在 (返回True或False)【适用在条件判断中if语句】
print(config.has_option("concurrent","thread")) #查找concurrent存在的thread节
print(config.has_option("concurrent","aa"))  #查找concurrent不存在的aa元素
print(config.has_option("concurrent","kong"))  #查找存在的kong节,只有k ,没有v(kong= )
print(config.has_section("db"))  #查找存在的db节
print(config.has_section("1b"))  #查找不存在的1b节

# 添加,先判断节是否存在,若不存在添加节default
if not config.has_section("default"):
    config.add_section("default")
if not config.has_option("default","db_host"):
    config.set("default","db_host","1.1.1.1")
config.write(open("cf.ini","w"))  #写入ini配置文件(易错点)

print(config.items("default"))

# 删除某个节并查看所有节
config.remove_section("default")
config.write(open("cf.ini","w"))
print(config.sections())



http://www.kler.cn/news/161646.html

相关文章:

  • 学习-ES
  • 三层交换机配置DHCP服务
  • 在vue中深度选择器的使用
  • 什么是css初始化
  • 代客泊车手势召车功能设计规范
  • 【计算机网络学习之路】HTTP响应报文Cookie原理
  • 玩转Sass:掌握数据类型!
  • postgreSql逻辑复制常用语句汇总和说明
  • SQL Server权限管理与数据恢复
  • Spring Boot HTTP 400 错误的日志信息在哪里查看 ?
  • 互联网洗鞋上门预约小程序预约下单系统源码公众号源码H5
  • 创建vue项目:node.js下载安装、配置环境变量,下载安装cnpm,配置npm的目录、镜像,安装vue、搭建vue项目开发环境(保姆级教程一)
  • office办公技能|ppt插件使用
  • 要求CHATGPT高质量回答的艺术:提示工程技术的完整指南—第 22 章:情感分析提示
  • QT作业1
  • vscode eide arm-gcc 编译环境搭建调试
  • springboot集成cxf
  • 【开源】基于JAVA的个人健康管理系统
  • 华为配置Smart Link负载分担示例
  • Hadoop3.x完全分布式环境搭建Zookeeper和Hbase
  • QT----Visual Studio打开.ui文件报错无法打开
  • debian11,debian 如何删除虚拟内存,交换分区
  • 【Https】HTTPS协议 的介绍及作用
  • Sql Server关于表的建立、修改、删除
  • Appium 并行测试多个设备
  • 【.NET Core】Linq查询运算符(二)
  • 5组10个共50个音频可视化效果PR音乐视频制作模板
  • 制作一个RISC-V的操作系统五-RISC-V汇编语言编程二
  • docker build构建报错:shim error: docker-runc not installed on system
  • 利用 Python 进行数据分析实验(五)