简体   繁体   中英

Convert incorrectly indented YAML file to python dictionary

There is a yaml file which is having wrong indentation like below:

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:

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? or convert to dict no matter how yaml is indented?

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. The file you show does not match the YAML grammar and thus is not YAML . It is not a bit not YAML, but not at all .

If you want to load a file regardless of indentation, this has nothing to do with YAML anymore. You need to define a grammar, possibly derived from YAML, that does understand your file, and then you need to implement it.

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.

So the realistic solution is to require whoever is supplying your input to give you a valid YAML file. Anything else is far too much effort.

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