如何从YAML文件中读取多个文档?

时间:2023-01-14 14:01:02

I want to make a YAML file that consists of only hashes. However, I cannot iterate over it. When I try to load the YAML file with:

我想创建一个只包含哈希的YAML文件。但是,我不能对它进行迭代。当我尝试加载YAML文件时:

YAML.load_file('yamlFile.yml') 

it returns only the first hash in the file. Here is an example file I would like to create:

它只返回文件中的第一个散列。下面是我想创建的一个示例文件:

---
:reach_hypo: true
:liquid: true
---
:reach_hypo: true
:liquid: false
---
:reach_hypo: true
:liquid: true 

If I load the above file, I get:

如果我加载上面的文件,我得到:

{reach_hypo: true, liquid: true}

The only workaround I have found is to add all the hashes into an array, then write it to the YAML file. Is there a better way, such as a YAML method, to iterate over the YAML file?

我找到的唯一方法是将所有的散列添加到一个数组中,然后将其写到YAML文件中。有没有更好的方法来迭代YAML文件,比如YAML方法?

3 个解决方案

#1


7  

Read Multiple YAML Documents From Single File as Streams

You can use YAML::load_stream to read multiple documents from a single file. For example:

可以使用YAML::load_stream从一个文件中读取多个文档。例如:

require 'yaml'

array = []
YAML.load_stream(File.read 'test.yml') { |doc| array << doc }
array

#=> [{:reach_hypo=>true, :liquid=>true}, {:reach_hypo=>true, :liquid=>false}, {:reach_hypo=>true, :liquid=>true}]

#2


0  

I imagine you know this and just want a different solution, but for the record I believe you have exactly two choices.

我想你知道这一点,只是想要一个不同的解决方案,但我郑重声明,我相信你有两个选择。

  1. Make an array of hashes.
  2. 制作一系列的散列。
    - a: b
      c: d
    - e: f
      g: h
  1. Use a two level hash, i.e, name each second-level hash and iterate over the keys in the top level.
  2. 使用一个二级哈希,i。e,为每一个二级散列命名,并对顶层的键进行迭代。
    x:
      a: b
      c: d
    y:
      e: f
      g: h

#3


0  

You can try using Psych. It has been around since 1.9.3 but i didn't notice it until today. uses libyaml for parsing. ruby-doc

你可以试试用心理学。从1。9.3开始,我就注意到了。使用libyaml解析。ruby-doc

I tried the following code and works as expected:

我尝试了以下代码并按预期工作:

require 'psych'

hash_arr = Psych.load_stream(File.read('yamlFile.yml'))
#=> [{:reach_hypo=>true, :liquid=>true}, {:reach_hypo=>true, :liquid=>false}, {:reach_hypo=>true, :liquid=>true}]

#1


7  

Read Multiple YAML Documents From Single File as Streams

You can use YAML::load_stream to read multiple documents from a single file. For example:

可以使用YAML::load_stream从一个文件中读取多个文档。例如:

require 'yaml'

array = []
YAML.load_stream(File.read 'test.yml') { |doc| array << doc }
array

#=> [{:reach_hypo=>true, :liquid=>true}, {:reach_hypo=>true, :liquid=>false}, {:reach_hypo=>true, :liquid=>true}]

#2


0  

I imagine you know this and just want a different solution, but for the record I believe you have exactly two choices.

我想你知道这一点,只是想要一个不同的解决方案,但我郑重声明,我相信你有两个选择。

  1. Make an array of hashes.
  2. 制作一系列的散列。
    - a: b
      c: d
    - e: f
      g: h
  1. Use a two level hash, i.e, name each second-level hash and iterate over the keys in the top level.
  2. 使用一个二级哈希,i。e,为每一个二级散列命名,并对顶层的键进行迭代。
    x:
      a: b
      c: d
    y:
      e: f
      g: h

#3


0  

You can try using Psych. It has been around since 1.9.3 but i didn't notice it until today. uses libyaml for parsing. ruby-doc

你可以试试用心理学。从1。9.3开始,我就注意到了。使用libyaml解析。ruby-doc

I tried the following code and works as expected:

我尝试了以下代码并按预期工作:

require 'psych'

hash_arr = Psych.load_stream(File.read('yamlFile.yml'))
#=> [{:reach_hypo=>true, :liquid=>true}, {:reach_hypo=>true, :liquid=>false}, {:reach_hypo=>true, :liquid=>true}]