安裝python yml 插件
pip install pyyaml -i https://mirrors.ustc.edu.cn/pypi/web/simple/
編寫配置文件 config.yaml
ip: 127.0.0.1
port: 5003
appKey: 32b2f148-a0c7-40ef-a7e2-5c25fa23b702
程序讀取配置
import yaml
yamlPath = 'config.yaml'
config = [];
#讀取配置文件
def loadConfig():
with open(yamlPath,'rb') as f:
# yaml文件通過---分節,多個節組合成一個列表
date = yaml.safe_load_all(f)
# salf_load_all方法得到的是一個迭代器,需要使用list()方法轉換爲列表
config=(list(date));
return config;
#從配置文件獲得指定key的值
def getConfig(key):
config=loadConfig()
for item in config:
if not item[key] is None:
return item[key];
#調用
port=getConfig("port");