poi 3.11版本匯出Excel報表
阿新 • • 發佈:2020-12-19
一、安裝模組
pip install pyyaml
二、host.yaml檔案
k8s:
- role: node1
ip: 192.168.1.2
hostname: k8s-node1
- role: node2
ip: 192.168.1.3
hostname: k8s-node2
DB:
ip: 192.168.1.10
port: 3306
三、read_yaml.py
#!/bin/env python
# -*- coding:utf-8 -*-
import yaml
import os
script_path = os.path.dirname(os.path.realpath(__file__)) #指令碼所在路徑
yaml_path = os.path.join(script_path, "host.yaml") #yaml檔案路徑
# open方法開啟直接讀出來
cfg = open(yaml_path, 'r').read() # 讀出來是字串
host_info = yaml.load(cfg,Loader=yaml.FullLoader) # 用load方法轉為字典
# print(host_info)
print("node1的IP地址:%s" % host_info.get('k8s')[0].get('ip')) #獲取node1的IP地址
print("DB的埠:%s" % host_info.get('DB').get('port'))