代码之家  ›  专栏  ›  技术社区  ›  skorada

在yaml文件中使用docstring定义:解析错误

  •  0
  • skorada  · 技术社区  · 7 年前

    \- api:
    
      api_first: """this is some docstring """
    

    In [1]: import yaml
    
    In [2]:with open('new.yaml') as f:
    ...:     dataMap = yaml.safe_load(f)
    ---------------------------------------------------------------------------
    ParserError                               Traceback (most recent call last)
    <ipython-input-2-2266b3e8606a> in <module>()
      1
      2 with open('new.yaml') as f:
      ----> 3     dataMap = yaml.safe_load(f)
    /phaedrus/home/skorada/lib/python3.5/site-
     packages/yaml/parser.py in parse_block_sequence_entry(self)
    391             token = self.peek_token()
    392             raise ParserError("while parsing a block collection", 
    self.marks[-1],
    --> 393                     "expected <block end>, but found %r" % token.id, 
    token.start_mark)
    394         token = self.get_token()
    395         event = SequenceEndEvent(token.start_mark, token.end_mark)
    
    ParserError: while parsing a block collection
    in "new.yaml", line 1, column 1
    expected <block end>, but found '?'
    in "new.yaml", line 2, column 1
    

    真的不确定问题出在哪里?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Anthon    7 年前

    YAML中没有三重引号。标量可以是单引号、双引号、非引号或(块样式)文字或折叠。

    如果要模拟Python的三重引号字符串,并且没有任何需要转义的控制字符(不包括换行符),可以使用块样式的文字标量:

    api:
    
      api_first: |
         this is some docstring 
         with newlines after each line
    

    \n

    你的 """this is some docstring """ 被解释为一行上的三个标量( "" , "this is some docstring " )这是无效的YAML。

    我假设开始 \