Ruby 如何将文件中的数据转换为嵌套哈希或哈希数组

Ruby 如何将文件中的数据转换为嵌套哈希或哈希数组,ruby,hash,Ruby,Hash,我有一个以下格式的文件。从文件中的记录1开始,我有大约100条记录。以下是该文件中的一些记录: Record 1: [record acct_num "11111111" trxn_dt "20140822" post_dt "20140822" trxn_amt "33.4" trxn_cat "RCI" trxn_cat_desc "ABCD"] Record 2: [record

我有一个以下格式的文件。从文件中的记录1开始,我有大约100条记录。以下是该文件中的一些记录:

Record 1:

[record   
  acct_num      "11111111"  
  trxn_dt       "20140822"   
  post_dt       "20140822"  
  trxn_amt      "33.4"  
  trxn_cat      "RCI"  
  trxn_cat_desc "ABCD"]

Record 2:

[record  
  acct_num      "44444444"  
  trxn_dt       "20140819"  
  post_dt       "20140822"  
  trxn_amt      "183.75"  
  trxn_cat      "ACI"  
  trxn_cat_desc "DEFG"]  
temp_file_output = File.open('file.txt', 'r')                                                     
  temp_file_output.readlines.each do |line|                                                 
    if line.match(/record/)                                                                        
      new_line = line.next                                             
    key, value = new_line.chomp.split(/"/)      
    file_read_hash[key] = value  
    end  
  end
我想将此文件中的数据转换为嵌套哈希或哈希数组,以便执行断言。我想要一个嵌套哈希,如:

{
  {
    acct_num => "11111111",
    trxn_dt => "20140822",
    post_dt => "20140822",
    trxn_amt => "33.4",
    trxn_cat => "RCI",
    trxn_cat_desc => "ABCD"
  },
  {
    acct_num => "44444444",
    trxn_dt => "20140819",
    post_dt => "20140822",
    trxn_amt => "183.75",
    trxn_cat => "ACI",
    trxn_cat_desc => "DEFG"
  }
}
temp_file_output = File.open('file.txt', 'r')                                                     
  temp_file_output.readlines.each do |line|                                                 
    if line.match(/record/)                                                                        
      new_line = line.next                                             
    key, value = new_line.chomp.split(/"/)      
    file_read_hash[key] = value  
    end  
  end
任何提示都会有帮助

temp_file_output = File.open('file.txt', 'r')                                                     
  temp_file_output.readlines.each do |line|                                                 
    if line.match(/record/)                                                                        
      new_line = line.next                                             
    key, value = new_line.chomp.split(/"/)      
    file_read_hash[key] = value  
    end  
  end
我的错。我没有上传我尝试过的代码。谢谢你让我知道。我是ruby新手,到目前为止我一直在尝试:

temp_file_output = File.open('file.txt', 'r')                                                     
  temp_file_output.readlines.each do |line|                                                 
    if line.match(/record/)                                                                        
      new_line = line.next                                             
    key, value = new_line.chomp.split(/"/)      
    file_read_hash[key] = value  
    end  
  end
好的,所以今天我尝试重新访问我的代码,并做了以下更改。我能够将第一个元素写入哈希。仍在研究如何将剩余元素包含到哈希中。有什么建议吗?这是我的密码:

temp_file_output = File.open('file.txt', 'r')                                                     
  temp_file_output.readlines.each do |line|                                                 
    if line.match(/record/)                                                                        
      new_line = line.next                                             
    key, value = new_line.chomp.split(/"/)      
    file_read_hash[key] = value  
    end  
  end
file_read_hash = {}  
   temp_file_output = File.open('sample.txt', 'r')  
   while !temp_file_output.eof?  
      line = temp_file_output.readline  
      if line.include?("record")  
        new_line = temp_file_output.readline.next  
          key,value = new_line.chomp.split(/"/)  
          file_read_hash[key.strip] = value  
          break if line.include?("]")  
        end  
      end  
   puts file_read_hash  

输出:{“rtr\u acct\u num”=>“4444”}

要拥有有效的Ruby对象,您需要如下内容:

temp_file_output = File.open('file.txt', 'r')                                                     
  temp_file_output.readlines.each do |line|                                                 
    if line.match(/record/)                                                                        
      new_line = line.next                                             
    key, value = new_line.chomp.split(/"/)      
    file_read_hash[key] = value  
    end  
  end
records = [
  [ [:acct_num,      "11111111"],  
    [:trxn_dt,       "20140822"],   
    [:trxn_cat_desc, "ABCD"]
  ],
  [ [:acct_num,      "44444444"],
    [:trxn_dt,       "20140819"],  
    [:trxn_cat_desc, "DEFG"]
  ]
]  

records.map(&:to_h)
  #=> [{:acct_num=>"11111111",
  #     :trxn_dt=>"20140822",
  #     :trxn_cat_desc=>"ABCD"},
  #    {:acct_num=>"44444444",
  #     :trxn_dt=>"20140819",
  #     :trxn_cat_desc=>"DEFG"}] 
您可以将
map(&:to_h)
看作是以下内容的简写:

temp_file_output = File.open('file.txt', 'r')                                                     
  temp_file_output.readlines.each do |line|                                                 
    if line.match(/record/)                                                                        
      new_line = line.next                                             
    key, value = new_line.chomp.split(/"/)      
    file_read_hash[key] = value  
    end  
  end
records.map { |a| a.to_h }
我们是在Ruby v2.0中获得的。对于早期版本,请使用类方法:

temp_file_output = File.open('file.txt', 'r')                                                     
  temp_file_output.readlines.each do |line|                                                 
    if line.match(/record/)                                                                        
      new_line = line.next                                             
    key, value = new_line.chomp.split(/"/)      
    file_read_hash[key] = value  
    end  
  end

您期望的“哈希”无效。在Ruby中不可能获得这样的对象。另外,在您期望的“哈希”中,
acct\u num
等的值是什么?欢迎使用堆栈溢出。通常情况下,只需提供您编写的代码的一个最小示例,并解释为什么它无法完成工作以及您认为问题出在哪里。如果没有这些,看起来你想让我们为你编写代码,但我们不会这样做。谢谢你让我知道。糟糕的是,我没有上传我第一次尝试的内容。我现在修改了我的问题,包括我尝试过的内容。好的。你想得到一些不知名的徽章不,只是取消@sawa的投票:
10-5*2#=>0
。还剩四条。为了解释上述评论,我最初在回答的开头加了一句话:“这是一条扩展评论。请不要向上投票或打勾。”这句话由@halfer编辑掉,我同意。我能够将文件的所有内容读入哈希数组。