【问题标题】:raise ScannerError(None, None, yaml.scanner.ScannerError: mapping values are not allowed hereraise ScannerError(None, None, yaml.scanner.ScannerError: 此处不允许映射值
【发布时间】:2021-06-29 10:09:08
【问题描述】:

我正在尝试更加熟悉 Python 的 .yaml 文件格式。我选择了一个基本玩具并开始玩它。但是,我最初遇到了一个问题。我搜索了解决方案并尝试了 Google 上提供的几个示例,但均未成功。 这是我的代码

test.yaml

这是我的 main.py

    import yaml
if __name__ == '__main__':

    stream = open("test.yaml", 'r')
    dictionary = yaml.load(stream)
    for key, value in dictionary.items():
        print (key + " : " + str(value))

错误

"C:\Users\Nafees Ahmed\AppData\Local\Programs\Python\Python38\python.exe" "C:/Users/Nafees Ahmed/PycharmProjects/Code_Playing_Testing/main.py"
C:/Users/Nafees Ahmed/PycharmProjects/Code_Playing_Testing/main.py:10: YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details.
  dictionary = yaml.load(stream)
Traceback (most recent call last):
  File "C:/Users/Nafees Ahmed/PycharmProjects/Code_Playing_Testing/main.py", line 10, in <module>
    dictionary = yaml.load(stream)
  File "C:\Users\Nafees Ahmed\AppData\Local\Programs\Python\Python38\lib\site-packages\yaml\__init__.py", line 114, in load
    return loader.get_single_data()
  File "C:\Users\Nafees Ahmed\AppData\Local\Programs\Python\Python38\lib\site-packages\yaml\constructor.py", line 49, in get_single_data
    node = self.get_single_node()
  File "C:\Users\Nafees Ahmed\AppData\Local\Programs\Python\Python38\lib\site-packages\yaml\composer.py", line 36, in get_single_node
    document = self.compose_document()
  File "C:\Users\Nafees Ahmed\AppData\Local\Programs\Python\Python38\lib\site-packages\yaml\composer.py", line 55, in compose_document
    node = self.compose_node(None, None)
  File "C:\Users\Nafees Ahmed\AppData\Local\Programs\Python\Python38\lib\site-packages\yaml\composer.py", line 84, in compose_node
    node = self.compose_mapping_node(anchor)
  File "C:\Users\Nafees Ahmed\AppData\Local\Programs\Python\Python38\lib\site-packages\yaml\composer.py", line 127, in compose_mapping_node
    while not self.check_event(MappingEndEvent):
  File "C:\Users\Nafees Ahmed\AppData\Local\Programs\Python\Python38\lib\site-packages\yaml\parser.py", line 98, in check_event
    self.current_event = self.state()
  File "C:\Users\Nafees Ahmed\AppData\Local\Programs\Python\Python38\lib\site-packages\yaml\parser.py", line 428, in parse_block_mapping_key
    if self.check_token(KeyToken):
  File "C:\Users\Nafees Ahmed\AppData\Local\Programs\Python\Python38\lib\site-packages\yaml\scanner.py", line 116, in check_token
    self.fetch_more_tokens()
  File "C:\Users\Nafees Ahmed\AppData\Local\Programs\Python\Python38\lib\site-packages\yaml\scanner.py", line 223, in fetch_more_tokens
    return self.fetch_value()
  File "C:\Users\Nafees Ahmed\AppData\Local\Programs\Python\Python38\lib\site-packages\yaml\scanner.py", line 577, in fetch_value
    raise ScannerError(None, None,
yaml.scanner.ScannerError: mapping values are not allowed here
  in "test.yaml", line 2, column 10

【问题讨论】:

    标签: python yaml


    【解决方案1】:

    在 YAML 映射中,每个键都只有一个值。键 foo 不能同时具有标量 bar pleh: 开头的嵌套映射作为值。

    目前尚不清楚您要做什么,但以下任何一种方法都可以:

    # dropping the value `bar`
    foo:
      pleh: help
      stuff:
        foo: bar
        bar: foo
    
    # making `bar` a value of the nested mapping
    foo:
      name: bar
      pleh: help
      stuff:
        foo: bar
        bar: foo
    
    # assuming `bar` and `pleh` should be a single multiline value.
    # this requires the explicit key marker `?`.
    foo:
      ? bar
        pleh
      : help
      stuff:
        foo: bar
        bar: foo
    

    【讨论】:

    • 感谢您的回答。我从这个链接得到了这个例子 = cloudbees.com/blog/….
    • @Ahmad 该教程充满了缩进错误。我建议避免它。
    • 非常感谢,请您分享python编程的任何真实来源
    • @Ahmad 抱歉,我不策划 Python YAML 教程,这也不是 StackOverflow 的目的。但是为什么不从official documentation 开始呢?
    猜你喜欢
    • 2019-08-16
    • 2012-02-21
    • 2015-12-14
    • 1970-01-01
    • 2021-05-02
    • 2014-12-13
    • 2019-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多