我怎么能读YAML文件?

时间:2023-01-23 17:01:03

I have such a YAML file:

我有这样一个YAML文件:

Company1:
  name: Something1
  established: 2000
#
Company2:
  name: Something2
  established: 1932

reading the YAML file: (** UPDATE **)

阅读YAML文件:(**更新**)

    config = YAML.load_file('file.yaml')
    config.each do |key, value|
     if(key == 'name')
      company_name = value
      #year = config['Company1']['established']
      year = config.fetch(key)['established']
     end
   end

** UPDATE ** Now the above code is working, but it shows the result as:

**更新**现在上面的代码正常工作,但它显示的结果如下:

 company1 =>  {"name" => "something1"} => {"established year" => 2000"}

how can I remove the the {} and "" ?

如何删除{}和“”?

2 个解决方案

#1


32  

Okay, so this is your YAML file right?

好的,这是你的YAML文件吗?

Company1:
  name: Something1
  established: 2000

Company2:
  name: Something2
  established: 1932

Okay now this YAML file actually represents a Hash. The has has two keys i.e Company1, Company2 (because they are the leading entries and the sub entries (name and established) are indented under them). The value of these two keys is again a Hash. This Hash also has 2 keys namely name and established. And they have values like Something1 and 2000 respectively etc.

好的,现在这个YAML文件实际上代表了一个哈希。有两个键,即Company1,Company2(因为它们是主要条目,子条目(名称和已建立)在它们下面缩进)。这两个键的值再次是哈希。这个Hash也有2个键,即name和established。它们的值分别为Something1和2000等。

So when you do,

所以当你这样做的时候

config=YAML.load_file('file.yml')

And print config (which is a Hash representing the YAML file contents) using,

并使用打印配置(表示YAML文件内容的Hash),

puts config

you get following output:

你得到以下输出:

{"Company1"=>{"name"=>"Something1", "established"=>2000}, "Company2"=>{"name"=>"Something2", "established"=>1932}}

So we have a Hash object as described by the YAML file.

所以我们有一个Hash对象,如YAML文件所描述的那样。

Using this Hash is pretty straight forward.

使用这个Hash非常简单。

Since each company's name and year come in a separate hash held by the outer hash (company1, company2), we can iterate through the companies. The following Code prints the Hash.

由于每个公司的名称和年份都是由外部哈希(company1,company2)保存的单独哈希,我们可以遍历这些公司。以下代码打印哈希。

config.each do |company,details|
  puts company
  puts "-------"
  puts "Name: " + details["name"]
  puts "Established: " + details["established"].to_s
  puts "\n\n"
end

So in Each iteration we get access to each (key,value) of the Hash. This in first iteration we have company(key) as Company1 and details(value) as {"name"=>"Something1", "established"=>2000}

因此,在每次迭代中,我们都可以访问Hash的每个(键,值)。在第一次迭代中,我们将公司(密钥)作为Company1,将详细信息(值)作为{“name”=>“Something1”,“established”=> 2000}

Hope this helped.

希望这有帮助。

#2


4  

YAML uses indentation for scoping, so try, e.g.:

YAML使用缩进来确定范围,所以试试,例如:

Company1:
  name: Something1
  established: 2000

Company2:
  name: Something2
  established: 1932

#1


32  

Okay, so this is your YAML file right?

好的,这是你的YAML文件吗?

Company1:
  name: Something1
  established: 2000

Company2:
  name: Something2
  established: 1932

Okay now this YAML file actually represents a Hash. The has has two keys i.e Company1, Company2 (because they are the leading entries and the sub entries (name and established) are indented under them). The value of these two keys is again a Hash. This Hash also has 2 keys namely name and established. And they have values like Something1 and 2000 respectively etc.

好的,现在这个YAML文件实际上代表了一个哈希。有两个键,即Company1,Company2(因为它们是主要条目,子条目(名称和已建立)在它们下面缩进)。这两个键的值再次是哈希。这个Hash也有2个键,即name和established。它们的值分别为Something1和2000等。

So when you do,

所以当你这样做的时候

config=YAML.load_file('file.yml')

And print config (which is a Hash representing the YAML file contents) using,

并使用打印配置(表示YAML文件内容的Hash),

puts config

you get following output:

你得到以下输出:

{"Company1"=>{"name"=>"Something1", "established"=>2000}, "Company2"=>{"name"=>"Something2", "established"=>1932}}

So we have a Hash object as described by the YAML file.

所以我们有一个Hash对象,如YAML文件所描述的那样。

Using this Hash is pretty straight forward.

使用这个Hash非常简单。

Since each company's name and year come in a separate hash held by the outer hash (company1, company2), we can iterate through the companies. The following Code prints the Hash.

由于每个公司的名称和年份都是由外部哈希(company1,company2)保存的单独哈希,我们可以遍历这些公司。以下代码打印哈希。

config.each do |company,details|
  puts company
  puts "-------"
  puts "Name: " + details["name"]
  puts "Established: " + details["established"].to_s
  puts "\n\n"
end

So in Each iteration we get access to each (key,value) of the Hash. This in first iteration we have company(key) as Company1 and details(value) as {"name"=>"Something1", "established"=>2000}

因此,在每次迭代中,我们都可以访问Hash的每个(键,值)。在第一次迭代中,我们将公司(密钥)作为Company1,将详细信息(值)作为{“name”=>“Something1”,“established”=> 2000}

Hope this helped.

希望这有帮助。

#2


4  

YAML uses indentation for scoping, so try, e.g.:

YAML使用缩进来确定范围,所以试试,例如:

Company1:
  name: Something1
  established: 2000

Company2:
  name: Something2
  established: 1932