简体   繁体   English

python - 将带有正确缩进的嵌套字典转储到 yaml

[英]python - dumping a nested dict with correct indentation to yaml

I'm painfully close to automating the dump of a yml file that's created from a data frame for an automation task.我非常接近于自动转储从数据框创建的 yml 文件以用于自动化任务。

I have a function that is structured like the following:我有一个结构如下的函数:

def get_all_values(nested_dictionary):
    for key,value in nested_dictionary.items():
        model = {
           "models": [
                {
            "name": key,
              "columns": None
                }
            ]
        }            
        yield(model)
        for key,value in value.items():
                table = [
                    {
                       "name": key,
                            "tests": [            
                                "not_null",
                                "unique"            
                            ]
                       }
                ]
                yield(table)
    nested_dictionary = d1
    get_all_values(nested_dictionary)
    data = get_all_values(nested_dictionary)
    with open('data.yml', 'w') as outfile:
        with redirect_stdout(outfile):
            for i in data:
                ruamel.yaml.round_trip_dump(i,outfile, indent=5, block_seq_indent=2)

The dict it references is yielded as a generator.它引用的字典作为生成器产生。 The dicts structure is:字典结构是:

    {'models': [{'name': 'budgets_sales', 'columns': None}]}
[{'name': 'budget_amt', 'tests': ['not_null', 'unique']}]
[{'name': 'budget_group', 'tests': ['not_null', 'unique']}]
[{'name': 'budget_name', 'tests': ['not_null', 'unique']}]
[{'name': 'budget_pk', 'tests': ['not_null', 'unique']}]
        

This works "well'...but the output is as follows:这工作“很好”......但输出如下:

models:
  -  name: budgets_sales
     columns:
  -  name: budget_amt
     tests:
       -  not_null
       -  unique
  -  name: budget_group
     tests:
       -  not_null
       -  unique
  -  name: budget_name
     tests:
       -  not_null
       -  unique

I require all values to keys in the dict to have an additional indentation.我要求字典中键的所有值都有一个额外的缩进。 I cannot figure out how to make the values indented against the keys.我无法弄清楚如何使值对键缩进。

It would look like this if correct:如果正确,它看起来像这样:

- name: budgets_sales
  columns:
      -  name: budget_amt
         tests:
            -  not_null
            -  unique
      -  name: budget_group
         tests:
            -  not_null
            -  unique
      -  name: budget_name
         tests:
            -  not_null
            -  unique
      -  name: budget_pk
         tests:
            -  not_null
            -  unique
      -  name: entry_type_code
         tests:
            -  not_null
            -  unique
      -  name: institution_fk
         tests:
            -  not_null
            -  unique

Could anyone provide an approach?任何人都可以提供一种方法吗?


Thanks to Anthon, this is what I ended up using:感谢 Anthon,这就是我最终使用的:

def get_all_values(nested_dictionary):
    res = [{"version":2},{"models":None}]
    for key,value in nested_dictionary.items():
        seq = []
        res.append([{"name": key, "columns": seq}])
        # for key1, value1 in value.items():  # not using value1
        for key1 in value.keys():
            elem = {"name": key1, "tests": ["not_null", "unique"]}
            seq.append(elem)
    return res

nested_dictionary = d1

get_all_values(nested_dictionary)

data = get_all_values(nested_dictionary)

    
with open('data.yml', 'w') as outfile:
    
    with redirect_stdout(outfile):
        
        for i in data:  
            
            yaml = ruamel.yaml.YAML()
            yaml.indent(mapping=5, sequence=5, offset=4)            
            yml.dump(i,outfile)

In your required output the value associated with key columns is a sequence.在您需要的输出中,与键columns关联的值是一个序列。 You only get that if your Python data structure is a list, so make sure you append your individual table entries to some variable.只有当您的 Python 数据结构是一个列表时,您才会得到它,因此请确保将您的各个table条目附加到某个变量中。

I am guessing d1 based on your "incorrect" output:我猜测d1基于您的“不正确”输出:

import sys
import ruamel.yaml

d1 = dict(budgets_sales=dict(budget_amt=None, budget_group=None, budget_name=None, budget_pk=None))

def get_all_values(nested_dictionary):
    res = []
    for key,value in nested_dictionary.items():
        seq = []
        res.append({"name": key, "columns": seq})
        # for key1, value1 in value.items():  # not using value1
        for key1 in value.keys():
            elem = {"name": key, "tests": ["not_null", "unique"]}
            seq.append(elem)
    return res
    
data = get_all_values(d1)

yaml = ruamel.yaml.YAML()
yaml.indent(mapping=5, sequence=5, offset=3)
yaml.dump(data, sys.stdout)

which gives:这使:

   - name: budgets_sales
     columns:
        - name: budgets_sales
          tests:
             - not_null
             - unique
        - name: budgets_sales
          tests:
             - not_null
             - unique
        - name: budgets_sales
          tests:
             - not_null
             - unique
        - name: budgets_sales
          tests:
             - not_null
             - unique

There are a few things you should take into account (apart from better formatting your code and data here on SO):您应该考虑一些事项(除了在 SO 上更好地格式化您的代码和数据):

  • the round_trip_dump function has been deprecated, don't use it in new code round_trip_dump函数已被弃用,请勿在新代码中使用
  • the recommended extension for files containing YAML documents has been .yaml , since at least Sep 2007包含 YAML 文档的文件的推荐扩展名是.yaml ,至少从 2007 年 9 月开始
  • don't write YAML files in multiple stages, create one complete data structure and dump it.不要在多个阶段编写 YAML 文件,创建一个完整的数据结构并转储它。 If you want multiple YAML documents in one file, make a list of data structures and use the .dump_all() method.如果您希望在一个文件中包含多个 YAML 文档,请列出数据结构并使用.dump_all()方法。

If all else fails and you have valid handcrafted YAML that you want to generate as output, load that YAML (with YAML(typ='safe').load() and inspect the data structure in Python that you get.如果所有其他方法都失败了,并且您有要生成为输出的有效手工 YAML,请加载该 YAML(使用YAML(typ='safe').load()并检查您获得的 Python 中的数据结构。

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

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