【问题标题】:Error while parsing YAML block of text解析 YAML 文本块时出错
【发布时间】:2011-08-28 08:05:45
【问题描述】:

我是 YAML 新手,并尝试执行 YAML 块,但我收到以下错误:

在扫描块映射时:预期,但发现:#(第 8 行,第 1 列)

虽然没有 | 也可以正常工作,但我希望保留空格。

YAML 文件(Home.yml):

---
section:
    title: About this site
    content:    |
        This is a test of a test test test test
        A very very good test 
        A test of test test 
section:
    title: Source code
    content: |
        Licens:: BSD
        Link:: Here
        Foo 
...

Ruby 代码:

home = YAML.load_file('data/Home.yml')
home.inspect

【问题讨论】:

    标签: ruby yaml


    【解决方案1】:

    您使用的是哪个 YAML 解析器? Ruby 1.8.7 解析器和 1.9 中的解析器都会解析您问题中的 YAML。

    仍然有麻烦,通过。您给出的语法是这样的哈希:

    {
      'section' => {
        'title' => "About this site",
        'content => ...
      }
      'section' => {
        'title' => 'Source code',
        'content' => ...
      }
    }
    

    但是,您不能让两个哈希键相同。发生的事情是最后一个获胜。您可能正在寻找一个哈希数组。为此,请使用以下 YAML 语法:

    ---
    -
      section:
        title: About this site
        content: |
            This is a test of a test test test test
            A very very good test
            A test of test test
    -
      section:
        title: Source code
        content: |
            Licens:: BSD
            Link:: Here
            Foo
    

    【讨论】:

    • 我发现 YAML 禁止制表符,而且因为它是一种空格,我不可能仅通过查看示例就知道。我确实将第二件事更正为列表,因为它只是我忘记了,但无论如何感谢您的帮助!
    • 我用的是默认的? IronRuby 1.1.2 之一,应该与 Ruby 1.9 兼容
    • @Yet Another Geek:哦,是的,标签:不断给予的礼物。我很高兴你把它理顺了。
    猜你喜欢
    • 2019-01-23
    • 2013-08-18
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    • 2023-03-19
    • 1970-01-01
    相关资源
    最近更新 更多