excel 转化为 yaml格式

需求:

  • 在工作中,经常需要将各种配置文件做处理,本次,将 Excel 数据处理成 yaml 格式,做一下记录,方便大家交流、以后回顾。
import os
import yaml
import pandas as pd


def excel2yaml():
	excel_info = pd.read_excel(".\xxx.xlsx", usecols=[0, 5, 12, 13], header=0, keep_default_na=False)
	info = excel_info.values
	# 获取 列名
	head_name = info[0]
	# 将表中的数据 按照 4 个产品 进行删选
	opk_info_new = []
	fac_info_new = []
	om_info_new = []
	srvc_info_new = []
	for i in info[1:]:
		# 处理第一列 路径补全的信息
		import re
		address = i[0]
		res = re.match("cloudos", address)
		# 路径补全,进行补全
		if res !=None:
			i[0] = "ha/ha/ha" + i[0]
		
		# 处理并区分 4 个 产品,以方便 后续不同 产品对应生成 对应的 yaml  文件
		i[1]="back_master"  #  处理该列的分支信息,赋值 新分支
		if i[2] == "fac":
			i[2] = "FaCoo"  # 第二列数值产品名 进行 重命名
			fac_info_dic = dict(zip(head_name, i))
			fac_info_new.append(fac_info_dic )
		elif i[2] == "opk":
			i[2] = "OpKoo" + i[2]
			opk_info_dic = dict(zip(head_name, i))
			opk_info_new.append(opk_info_dic )
		elif i[2] == "om":
			i[2] = "OaMoo" + i[2]
			om_info_dic = dict(zip(head_name, i))
			om_info_new.append(om_info_dic  )
		else:
			i[2] = "SaVoS" + i[2]
			srvc_info_dic = dict(zip(head_name, i))
			srvc_info_new .append(srvc_info_dic )

	product_lsit = [opk_info_new , fac_info_new , om_info_new , srvc_info_new  ]
	# 写入到 yaml 文件
	for single_product in product_lsit :
		yaml_file_name = single_product[0]["release"].replace(" ","_") 
		curpath = os.path.dirname(os.path.realpath(__file__))
		yamlpath = os.path.join(curpath, yaml_file_name + ".yaml")
		with open(yamlpath,"w") as f :
			yaml.safe_dump({"projects": single_product}, f, default_style=False, allow_unicode=True)


excel2yaml()

最终 ,将 Excel 中的数据分成4个yanl 文件

在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值