gpt4 book ai didi

python - 'yaml.parser.ParserError: expected ' <文档开始>', but found ' < block 映射开始>''是什么意思?

转载 作者:太空狗 更新时间:2023-10-29 21:34:25 34 4
gpt4 key购买 nike

我有以下 YAML 文件:

[mysqld]
user: "mysql"
pid-file: /var/run/mysqld/mysqld.pid
skip-external-locking
old_passwords: 1
skip-bdb
skip-innodb
create_key: yes
needs_agent: no
knows_oop: True
likes_emacs: TRUE
women:
- Mary Smith
- Susan Williams

和以下 Python 代码:

#!/usr/bin/env python

import yaml


with open("config.yml") as f:
sample_config = f.read()

print(yaml.load(sample_config))

但它给了我:

Traceback (most recent call last):
File "/home/moose/Desktop/bla.py", line 9, in <module>
print(yaml.load(sample_config))
File "/usr/local/lib/python2.7/dist-packages/yaml/__init__.py", line 71, in load
return loader.get_single_data()
File "/usr/local/lib/python2.7/dist-packages/yaml/constructor.py", line 37, in get_single_data
node = self.get_single_node()
File "/usr/local/lib/python2.7/dist-packages/yaml/composer.py", line 39, in get_single_node
if not self.check_event(StreamEndEvent):
File "/usr/local/lib/python2.7/dist-packages/yaml/parser.py", line 98, in check_event
[Finished in 0.1s with exit code 1]
[shell_cmd: python -u "/home/moose/Desktop/bla.py"]
[dir: /home/moose/Desktop]
[path: /usr/local/texlive/2013/bin/x86_64-linux:/home/moose/google-cloud-sdk/bin:/home/moose/Downloads/google_appengine:/usr/local/texlive/2013/bin/x86_64-linux:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games] self.current_event = self.state()
File "/usr/local/lib/python2.7/dist-packages/yaml/parser.py", line 174, in parse_document_start
self.peek_token().start_mark)
yaml.parser.ParserError: expected '<document start>', but found '<block mapping start>'
in "<string>", line 2, column 1:
user: "mysql"

我不知道是什么

expected '<document start>', but found '<block mapping start>'

手段和如何解决它。什么是 <document start>什么是 <block mapping start>

最佳答案

您的文件不是有效的 YAML。它看起来像是 YAML 和 INI file 的混合体.

  • 您不能在 YAML 中定义像 [mysql] 这样的 block 。如果要定义相关属性的集合,请使用带有嵌套键的列表:

    - service:
    name: mysql
    type: database
    port: 3306
    - service:
    name: ssh
    type: remote access
    port: 22
  • 您不能使用像 skip-external-locking 这样的裸词。每个属性都需要一个值。请改用 skip-external-locking: true

这是修复了语法错误的文档版本。我用 YAMLLint 检查了这个,一个用于验证 YAML 的便捷工具。

name: mysqld
user: mysql
pid-file: /var/run/mysqld/mysqld.pid
skip-external-locking: true
old_passwords: 1
skip-bdb: true
skip-innodb: true
create_key: yes
needs_agent: no
knows_oop: True
likes_emacs: TRUE
women:
- Mary Smith
- Susan Williams

关于python - 'yaml.parser.ParserError: expected ' <文档开始>', but found ' < block 映射开始>''是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24983938/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com