1 configparser模块
用于生成 和修改 常见配置文档 ,当前模块的名称在 python 3.x 版本中变更为 configparser。它提供类似于 Microsoft Windows INI 文件的结构。 ConfigParser允许编写可由最终用户轻松定制的 Python 程序。
配置文件由各部分组成 ,后跟选项的键/值对 。 段名用[] 字符分隔。 这些键/值对 用:或= 隔开。 注释以#或; 开头。
1、如下,好多软件的常见配置文件:
首先说明几个概念:
- 下面的
[bitbucket.org] 用[] 包围起来的是section ,可以理解为节点 - 节点下面的是
键值对 ,这个键就是option
然后再接着往下看就会明白section和option这个概念了!
注意:
DEFAULT 这个关键字不属于section !我也不太明白
[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes
[bitbucket.org]
user = hg
[topsecret.server.com]
host port = 50022
forwardx11 = no
2、使用configparser模块生成上面的配置文件
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['Host Port'] = '50022'
topsecret['ForwardX11'] = 'no'
config['DEFAULT']['ForwardX11'] = 'yes'
with open('config.ini', 'w') as configfile:
config.write(configfile)
生成config.ini 结果如下:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qqCtrHR1-1646310028648)(./imgs/cp1.png)]
3、读取配置文件
import configparser
def read_config():
config = configparser.ConfigParser()
config.read('config.ini', encoding='utf-8')
sections = config.sections()
print(sections)
items1 = config.items('DEFAULT')
items2 = config.items('bitbucket.org')
items3 = config.items('topsecret.server.com')
print(items1)
print(items2)
print(items3)
keys2 = config.options('bitbucket.org')
keys3 = config.options('topsecret.server.com')
print(keys2)
print(keys3)
'''
ConfigParserObject.get(section, option)
返回结果是字符串类型
ConfigParserObject.getint(section, option)
返回结果是int类型
ConfigParserObject.getboolean(section, option)
返回结果是bool类型
ConfigParserObject.getfloat(section, option)
返回结果是float类型
'''
user = config.get('bitbucket.org', 'user')
print(user, type(user))
interval = config.get('DEFAULT', 'serveraliveinterval')
print(interval, type(interval))
interval2 = config.getint('DEFAULT', 'serveraliveinterval')
print(interval2, type(interval2))
res1 = config.has_section('DEFAULT')
res2 = config.has_section('bitbucket.org')
print(res1, res2)
res3 = config.has_option('DEFAULT', 'forwardx11')
res4 = config.has_option('bitbucket.org', 'user')
print(res3, res4)
if __name__ == '__main__':
read_config()
>>> import configparser
>>> config = configparser.ConfigParser()
>>> config.sections()
[]
>>> config.read('example.ini')
['example.ini']
>>> config.sections()
['bitbucket.org', 'topsecret.server.com']
>>> 'bitbucket.org' in config
True
>>> 'bytebong.com' in config
False
>>> config['bitbucket.org']['User']
'hg'
>>> config['DEFAULT']['Compression']
'yes'
>>> topsecret = config['topsecret.server.com']
>>> topsecret['ForwardX11']
'no'
>>> topsecret['Port']
'50022'
>>> for key in config['bitbucket.org']: print(key)
...
user
compressionlevel
serveraliveinterval
compression
forwardx11
>>> config['bitbucket.org']['ForwardX11']
'yes'
3、configparser的增删改查语法
import ConfigParser
config = ConfigParser.ConfigParser()
config.read('i.cfg')
参考:https://www.jianshu.com/p/417738fc9960 参考:https://geek-docs.com/python/python-tutorial/python-configparser.html 参考:https://docs.python.org/3/library/configparser.html
2 PyYAML模块
1、PyYAML模块安装
pip install PyYAML
2、yaml文件的语法格式参考
- 参考:https://www.runoob.com/w3cnote/yaml-intro.html
- 参考:http://www.ruanyifeng.com/blog/2016/07/yaml.html
3、yaml使用
- 参考:https://shliang.blog.csdn.net/article/details/111591030
- 参考:https://geek-docs.com/python/python-tutorial/python-yaml.html
2.2 yaml基本使用
在yolov5 中就是通过yaml文件 来配置模型的定义:
参考:https://github.com/ultralytics/yolov5/blob/c72270c076e1f087d3eb0b1ef3fb7ab55fe794ba/models/yolo.py
2.2.1 使用yaml.load()读取yaml文件,并获取其中的配置信息
1、首先有一个配置文件config.yaml ,内容如下:
optimizer:
- SGD
- Adam
train:
optimization: Adam
learning_rate: 0.001
batch_size: 64
epoch: 200
2、读取yaml文件
data_dict = yaml.load(f, Loader=yaml.FullLoader) :把yaml文件解析成字典- 使用字典的key获取对应的值
import yaml
def read_yaml():
yaml_f = open('./config.yaml', 'r', encoding='utf-8')
data_dict = yaml.load(yaml_f, Loader=yaml.FullLoader)
print(type(data_dict))
print(data_dict)
lr = data_dict['train']['learning_rate']
print(lr, type(lr))
if __name__ == '__main__':
read_yaml()
2.2.2 使用yaml.load_all()读取多个yaml文档
1、使用配置文件docs.yaml如下:
我们在docs.yaml中有两个文档 。 文件用--- 分隔。(--- 加不加都是一样的,只是方便区分而已)
cities:
- Bratislava
- Kosice
- Trnava
- Moldava
- Trencin
---
companies:
- Eset
- Slovnaft
- Duslo Sala
- Matador Puchov
2、读取多个yaml文档
docs_generator = yaml.load_all(yaml_f, Loader=yaml.FullLoader) :读取得到的是一个迭代器- 迭代器中存储的是一个一个的字典数据,可以循环遍历获取
import yaml
def read_docs_yaml():
yaml_f = open('./docs.yaml', 'r', encoding='utf-8')
docs_generator = yaml.load_all(yaml_f, Loader=yaml.FullLoader)
print(type(docs_generator))
print(docs_generator)
for doc in docs_generator:
for key, value in doc.items():
print(key, '->', value)
if __name__ == '__main__':
read_docs_yaml()
2.2.3 把数据序列化成yaml格式,并写入到yaml文件
1、如下,把数据写入到yaml文件中
import yaml
def write_yaml():
users = [{'name': 'John Doe', 'occupation': 'gardener'},
{'name': 'Lucy Black', 'occupation': 'teacher'}]
data_str = yaml.dump(users)
print(type(data_str))
print(data_str)
'''
- name: John Doe
occupation: gardener
- name: Lucy Black
occupation: teacher
'''
with open('users.yaml', 'w') as f:
data = yaml.dump(users, f)
if __name__ == '__main__':
write_yaml()
生成users.yaml文件内容如下:
- name: John Doe
occupation: gardener
- name: Lucy Black
occupation: teacher
2、示例2
import yaml
def write_yaml2():
train_config = {'optimizer':['SGD', 'Adam'],
'train':{'optimization': 'Adam',
'learning_rate': 0.001,
'batch_size': 64,
'epoch': 200}}
with open('train_config.yaml', 'w') as f:
data = yaml.dump(train_config, f)
if __name__ == '__main__':
write_yaml2()
生成train_config.yaml文件内容如下:
optimizer:
- SGD
- Adam
train:
batch_size: 64
epoch: 200
learning_rate: 0.001
optimization: Adam
2.2.4 读取yaml文件,并对键值进行排序
1、items.yaml文件内容为:
raincoat: 1
coins: 5
books: 23
spectacles: 2
chairs: 12
pens: 6
2、对读取的yaml内容按照key进行排序
import yaml
def read_yaml_sorted_key():
with open('items.yaml', 'r') as f:
data_dict = yaml.load(f, Loader=yaml.FullLoader)
print(type(data_dict))
print(data_dict)
sorted = yaml.dump(data_dict, sort_keys=True)
print(type(sorted))
print(sorted)
'''
books: 23
chairs: 12
coins: 5
pens: 6
raincoat: 1
spectacles: 2
'''
with open('items_new.yaml', 'w') as f2:
yaml.dump(data_dict, f2, sort_keys=True)
if __name__ == '__main__':
read_yaml_sorted_key()
|