如何读取通用配置文件conf?
通用配置文件conf格式
大家经常见到各种各样的配置文件格式,有json格式(JavaScript Object Notation),ini格式(Initialization File)yml(Yet Another Markup Language)标记语言等等。
真实场景中常用于MySQL数据库my.cnf配置读取,或者在项目中进行配置文件读写操作等。
本次我们关注的是conf或者ini文件格式,举例如下:
[DEFAULT]
conf_str = name
dbn = mysql
user = root
host = localhost
port = 3306[db]
user = aaa
pw = ppp
db = example[db2]
host = 127.0.0.1
pw = www
db = example
如何读取呢?
本次以python语言读取案例,典型读取操作
from configparser import ConfigParser # 配置文件解析器def get_conf(path):"""get conf file:param path::return:"""cf = ConfigParser()cf.read(path)db1_user = cf.get("db", "user") # 读取db层级下的user值db1_pw = cf.get("db", "pw") # 读取另一个值db1_db = cf.get("DEFAULT", "dbn") # 读取Default区块下的值print(db1_user)print(db1_pw)print(db1_db)if __name__ == '__main__':get_conf("./format.conf")
aaa
ppp
mysql
读取配置文件写法还有另一种形式。
from configparser import ConfigParserdef get_conf(path):"""get conf file:param path::return:"""cf = ConfigParser()cf.read(path)print(cf.sections()) # 读取区块[信息]db1_user = cf["db"]["user"]db1_pw = cf["db"]["pw"]db1_db = cf["DEFAULT"]["db"]print(db1_user)print(db1_pw)print(db1_db)if __name__ == '__main__':get_conf("./format.conf")
['db', 'db2']
aaa
ppp
localhost
如何写入简单举例
至于写入实际上类似,先进行字典键值对组装,然后再写入文件结束。
import configparser
config = configparser.ConfigParser()
config['DEFAULT'] = {'ServerAliveInterval': '45','Compression': 'yes','CompressionLevel': '9'}
config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'
config['topsecret.server.com'] = {} # 定义空字典类型
topsecret = config['topsecret.server.com']
topsecret['Port'] = '50022' # 添加键值对
topsecret['ForwardX11'] = 'no'
config['DEFAULT']['ForwardX11'] = 'yes'with open('example.ini', 'w') as configfile: # 写入配置文件config.write(configfile)
结果example.ini内容如下:
[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes[bitbucket.org]
user = hg[topsecret.server.com]
port = 50022
forwardx11 = no
参考地址:https://docs.python.org/zh-cn/3.11/library/configparser.html#
如何读取通用配置文件conf?相关推荐
- Java读取Properties配置文件
目录 1.Properties类与Properties配置文件 2.Properties中的主要方法 3.示例 1.Properties类与Properties配置文件 Properties类继承自H ...
- go 读取 json 配置文件
引言 go 读取 json 配置文件,主要有两个知识点:一是文件的读取,二是 json 数据的处理. 序列化与反序列化 对 json 数据的处理往往指的是数据的序列化和反序列化. 把变量从内存中变成可 ...
- springboot 打jar 包部署时 读取外部配置文件
案例:本文主要描述linux系统执行jar包读取jar包同级目录的外部配置文件 方法一:相对路径设置配置文件 (1)在jar包同级目录创建配置文件conf.properties并写入配置数据: con ...
- Flex读取XML配置文件
在Flex中我们经常使用xml文件,因为Flex支持强大的E4X功能,读取xml相当简洁.总结一下常用的Flex读取XML配置文件的方法: 1.使用Model标签形式 首先声明Model标签, < ...
- Spring-boot中读取config配置文件的两种方式
了解过spring-Boot这个技术的,应该知道Spring-Boot的核心配置文件application.properties,当然也可以通过注解自定义配置文件的信息. Spring-Boot读取配 ...
- linux写参数文件,linux下纯C++读取参数配置文件
微软把程序猿养懒了.如果你在windows里写程序,那么本文你可以不看,有GetPrivateProfileString等来读取:如果你在qt里写程序,那么本文也可以不看.如果你在linux或者mac ...
- python 读取.env配置文件
python 读取.env配置文件 目录结构 project/ ├─config │ └─__init__.py | └─base.py | └─conf.py ├─.env ├─.env.examp ...
- 无需Java环境,Jar包一键启停(静默启停),开机自启,读取外部配置文件
无需Java环境,Jar包一键启停(静默启停),开机自启,读取外部配置文件 前言 一.目录结构 1.根目录 2.bin 目录 3.conf 目录 4.target 目录 二.文件内容 1.startu ...
- @PropertySource读取外部配置文件中的k-v保存到运行的环境变量中,加载完微博的配置文件以后使用${}取配置文件中的键值
@PropertySource读取外部配置文件中的k-v保存到运行的环境变量中,加载完微博的配置文件以后使用${}取配置文件中的键值 该注解value={},可以使用String数组形式,读取多个配置 ...
- 解决Spring boot中读取属性配置文件出现中文乱码的问题
解决Spring boot中读取属性配置文件出现中文乱码的问题 参考文章: (1)解决Spring boot中读取属性配置文件出现中文乱码的问题 (2)https://www.cnblogs.com/ ...
最新文章
- 支持的网卡列表_Windows 10的5G网卡折腾笔记(含采购链接)
- 图形处理(四)基于梯度场的网格编辑-Siggraph 2004
- 零售商的“基因改造”浪潮
- Silverlight3 Tools Download link
- oracle 删除时间段的,oracle SQL如何从日期中删除时间
- 【node】之nvm的使用
- C#Linq的10个练习
- python sklearn 绘制决策树模型的节点图
- 一、app更新提示后台接口开发-(2)数据库表设计
- 2020年java最新招聘_2021年北京Java 工程师招聘-北京Java 工程师招聘求职信息-拉勾招聘...
- TypeError: Layout of the output array image is incompatible with cv::Mat
- 7.jenkins 发布邮件
- TP-LINKTL-WR845N和腾达W311R怎么设置wds桥接
- 微信如何收发企业邮箱邮件
- esp32单片机入门学习
- DevExpress ASP.NET and Blazor图表编制
- OpenLayers6 裁切地图(Layer Clipping)
- C语言冒泡法输出成绩与学号,C语言:根据成绩进行冒泡排序,学号也得跟着动,输出的结果...
- 【数据结构】带你细致理解十大排序(附源码)
- 使用Springboot实现Nginx均衡负载功能