2017-09-06 903 views
0

我试图解析以下YAML文件:在YAML文件文档字符串定义:解析错误

\- api: 

    api_first: """this is some docstring """ 

我基本上要使用三引号,并具有在其中一些语句。 但是当我使用的YAML库,它抛出一些错误,我

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

YAML中没有三重引号。您的标量可以是单引号,双引号,不带引号或(以块格式)文字或折叠。

如果你想模仿Python的三引号字符串,你没有这样的需要转义的任何控制字符(那些不包括换行符),可以使用块风格的文字标量:

api: 

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

。如果您有控制字符,则必须在YAML中使用双引号标量,并且要么换行符为\n,要么在双引号内使用双换行符。

"""this is some docstring """被解释为在同一行("""this is some docstring """)三个标量和无效YAML。

我假设您的YAML中的起始\是一个错字。

+0

嗨Anthon,谢谢你的回复。我想我正在尝试多线。这可以通过使用“|”来解决它为我工作。 – skorada

+0

这是我在答案中指出的“块样式字面标量”。我应该包括一个例子。 – Anthon