How can I do string concatenation, or string replacement in YAML?

后端 未结 7 2255
深忆病人
深忆病人 2020-12-01 08:38

I have this:

user_dir: /home/user
user_pics: /home/user/pics

How could I use the user_dir for user_pics? If I have to specify

7条回答
  •  粉色の甜心
    2020-12-01 09:20

    string.join() won't work in Python3, but you can define a !join like this:

    import functools
    import yaml
    
    class StringConcatinator(yaml.YAMLObject):
        yaml_loader = yaml.SafeLoader
        yaml_tag = '!join'
        @classmethod
        def from_yaml(cls, loader, node):
            return functools.reduce(lambda a, b: a.value + b.value, node.value)
    
    c=yaml.safe_load('''
    user_dir: &user_dir /home/user
    user_pics: !join [*user_dir, /pics]''')
    print(c)
    

提交回复
热议问题