yaml 1.2
python中添加自定义constructor
# 使用 yaml 1.2 from ruamel.yaml import YAML yaml = YAML() ## define custom tag handler def join(loader, node): seq = loader.construct_sequence(node) return ''.join([str(i) for i in seq]) ## register the tag handler yaml.constructor.add_constructor('!join', join)
yaml格式
a: *anchor something b: !join ['We get ', *anchor]
python读取
with open('config.yaml', 'r', encoding='utf-8') as f: config = yaml.load(f) print(config['b']) # 可以看到拼接好的字符串为:'We get something'