从csv获取哈希元素后,无法访问这些元素

f4t66c6m  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(328)

我有一个cvs,我想访问这些值。如果我这样做:

require 'csv'   
    CSV.foreach(filename, headers: true) do |row|
      new_row = row.to_hash
      puts "#{new_row}"
    end

这就是结果-> {"user_id"=>"111", "sport"=>"aaa"} 但如果我这样做,什么都不会被打印出来

require 'csv'   
    CSV.foreach(filename, headers: true) do |row|
      new_row = row.to_hash
      puts "#{new_row["user_id"]}"
    end

为什么会这样?我怎样才能得到信息?

c6ubokkw

c6ubokkw1#

尝试使用符号而不是字符串来查找键

require 'csv'   
  CSV.foreach(filename, headers: true) do |row|
  new_row = row.to_hash
  puts new_row[:user_id]
end

相关问题