Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

YAML: multiple fields in one line

Tags:

yaml

pyyaml

How can I write multiple fields in one line Loader=yaml.FullLoader?

Eg. turn this:

field:
   item0: 0
   item1: 1

into this:

field:
  item0:0, item1:1

Or even:

field: {item0:0, item1:1}

Eg. in JSON you could do:

{"field":{ "item0":0, "item1":1 }}

How can you do the same thing in YAML?

like image 484
étale-cohomology Avatar asked May 01 '26 14:05

étale-cohomology


1 Answers

YAML is designed to be a superset of JSON, so your JSON one-liner would also work with a YAML parser:

import yaml

print(yaml.load('{"field":{ "item0":0, "item1":1 }}', Loader=yaml.FullLoader))

and since the field names are all alphabets in your example, you can omit the quotes:

print(yaml.load('{field: {item0: 0,item1: 1}}', Loader=yaml.FullLoader))

both would produce the output:

{'field': {'item0': 0, 'item1': 1}}
like image 57
blhsing Avatar answered May 05 '26 07:05

blhsing



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!