简体   繁体   English

将具有不同数据类型的字典放入 python 中的 yaml 文件中

[英]dictionary with different data types into yaml file in python

My end goal is to create a.yaml file that looks like this:我的最终目标是创建一个如下所示的.yaml 文件:

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正如您在评论中看到的那样 - 您应该使用 integer 100和 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.就这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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