在 yaml 文件中使用文档字符串定义:解析错误
Defining with docstrings in yaml file: Parsing errors
我正在尝试解析以下 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
真的不确定问题出在哪里?
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 中的开头 \
是一个拼写错误。
我正在尝试解析以下 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
真的不确定问题出在哪里?
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 中的开头 \
是一个拼写错误。