简体   繁体   English

将错误缩进的 YAML 文件转换为 python 字典

[英]Convert incorrectly indented YAML file to python dictionary

There is a yaml file which is having wrong indentation like below:有一个 yaml 文件的缩进错误,如下所示:

name:  testing
date:       2020-07-13
version:    1.0
targets:
  - sequence: 1
   name:     Book1
    author: abc
  - sequence:   2
    name:   Book2    
   author: xyz

which If I try to load it using pyYAML will get parser exception like:如果我尝试使用 pyYAML 加载它,则会得到解析器异常,例如:

yaml.parser.ParserError: while parsing a block collection
  in "E:/test.yaml", line 5, column 3
expected <block end>, but found '<block mapping start>'
  in "E:/test.yaml", line 6, column 4

How to convert this yaml with indentation problem to dict without manually fixing indentation?如何在不手动修复缩进的情况下将具有缩进问题的 yaml 转换为 dict? or convert to dict no matter how yaml is indented?或转换为 dict 无论 yaml 是如何缩进的?

Loading any kind of structured data always requires some kind of grammar specification, be it explicit via a specification document, or implicit by just writing the code loading it.加载任何类型的结构化数据总是需要某种语法规范,无论是通过规范文档显式,还是通过编写加载它的代码来隐式。

YAML has an explicit specification. YAML 有一个明确的规范。 The file you show does not match the YAML grammar and thus is not YAML .您显示的文件与 YAML 语法不匹配,因此不是 YAML It is not a bit not YAML, but not at all . YAML一点也不,但一点也不

If you want to load a file regardless of indentation, this has nothing to do with YAML anymore.如果您想加载文件而不考虑缩进,这与 YAML 无关。 You need to define a grammar, possibly derived from YAML, that does understand your file, and then you need to implement it.您需要定义一个语法,可能源自 YAML,它确实理解您的文件,然后您需要实现它。

You can write some sed or awk command to fix this particular file, but it can't easily be generalized because you need a proper YAML parser just to detect wrong indentation.您可以编写一些 sed 或 awk 命令来修复此特定文件,但它不容易被概括,因为您需要一个适当的 YAML 解析器来检测错误的缩进。

So the realistic solution is to require whoever is supplying your input to give you a valid YAML file.因此,现实的解决方案是要求提供您输入的任何人为您提供有效的 YAML 文件。 Anything else is far too much effort.其他任何事情都太过分了。

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

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