简体   繁体   中英

dictionary with different data types into yaml file in python

My end goal is to create a.yaml file that looks like this:

A:
  first_entry: 100
  second_entry: false
  third_entry: 10 * seed
B:
 seed: 1e5 * Volt

I tried this with a dictionary structure:

import yaml
my_dict = {"A": {"first_entry": "100", "second_entry": "false", "third_entry": "10 * seed"},
           "B": {"seed": "1e5 * Volt"},
         }
with open('output.yml', 'w') as outfile:
    yaml.dump(my_dict, outfile, default_flow_style = False)

this then leads to:

A:
  first_entry: '100'
  second_entry: 'false'
  third_entry: 10 * seed
B:
  seed: 1e5 * Volt

meaning somewhere, there is always some ' ' around either the strings or ints. Is there an easy/clever way to solve this? Any help is greatly appreciated!

As you could read in comments - you should use integer 100 and boolean False

"A": {"first_entry": 100, "second_entry": False, ...

and it will create expected result without ''

A:
  first_entry: 100
  second_entry: false

That's all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM