yaml 变量引用_python笔记49-yaml文件中变量的使用(锚点& 与 引用*)

前言

在yaml文件中如何引用变量?当我们在一个yaml文件中写很多测试数据时候,比如一些配置信息像用户名,邮箱,数据库配置等很多地方都会重复用到。

重复的数据,如果不设置变量,后续维护起来就很困难。

yaml文件里面也可以设置变量(锚点&),其它地方重复用到的话,可以用*引用

锚点&和引用*

对于重复的数据,可以单独写到yaml文件的开头位置,其它的地方用到的可以用*引用

# 作者-上海悠悠 QQ交流群:717225969

# blog地址 https://www.cnblogs.com/yoyoketang/

userinfo: &userinfo

user: yoyo

email: 283340479@qq.com

test1:

name: testcase 1

data:

<<:>

tel: 12345678901

test2:

name: testcase 2

data:

<<:>

tel: 15201234023

等同于下面的数据

userinfo:

user: yoyo

email: 283340479@qq.com

test1:

name: testcase 1

data:

user: yoyo

email: 283340479@qq.com

tel: 12345678901

test2:

name: testcase 2

data:

user: yoyo

email: 283340479@qq.com

tel: 15201234023

&用来建立锚点(userinfo),<

*引用value值

上面的例子是对userinfo整体的数据,引用到其它地方了,有时候我们只想引用其中的一个值,如email的值,如何实现呢?

# 作者-上海悠悠 QQ交流群:717225969

# blog地址 https://www.cnblogs.com/yoyoketang/

userinfo: &userinfo

user: yoyo

email: &email 283340479@qq.com

test3:

name: testcase 3

data:

user: admin

email: *email

tel: 12345678901

test4:

name: testcase 5

data:

user: test123

email: *email

tel: 12345678902

等同于下面的数据

userinfo:

user: yoyo

email: 283340479@qq.com

test3:

name: testcase 3

data:

user: admin

email: 283340479@qq.com

tel: 12345678901

test4:

name: testcase 5

data:

user: test123

email: 283340479@qq.com

tel: 12345678902

这样就可以把重复的数据,单独写到一个配置,其它地方*引用就可以了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值