Skip to content
Advertisement

Yaml, Is there any method to add some character after call the anchor

For example

My yaml file(Test.yml):

fruit  : &apple Apple
fruits : *apple , Pineapple

Python code

import yaml

with open('Test.yml', 'r') as f:
     data = yaml.safe_load(f)

print(data['fruits'])

Output get error

yaml.parser.ParserError: while parsing a block mapping
expected <block end>, but found ','

My expected output is

Apple, Pineapple

Advertisement

Answer

You can make your fruits be a sequence:

fruit: &apple Apple
fruits: [*apple, pineapple]

However you can not concatenate the scalar value Apple to other values, because YAML is a pure data serialization language and cannot process data in any way.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement