我有一个包含两列的CSV文件:
PPS_Id Amount
123 100
1234 150
我使用以下代码从这个文件中读取数据并将其插入到数组中:
CSV.foreach("filename.CSV", headers: true) do |row|
file_details << row.inspect # hash
end
我尝试将file_details
中的数据推入一个哈希表中,以PPS_Id
为键,Amount
为值。我使用以下代码:
file_details_hash = Hash.new
file_details.each { |x|
file_details_hash[x['PPS_Id']] = x['Amount']
}
但当我打印结果时,什么也没有,只有
{"PPS_Id"=>"Amount"}
。 请问你能帮忙吗?
Hash[*[["PPS_Id", "Amount"], ["123", "100"], ["1234", "150"]].flatten] => {"PPS_Id"=>"Amount", "123"=>"100", "1234"=>"150"}
- Roman Kiselenko