繁体   English   中英

从 csv 获取散列元素后无法访问它们

[英]Can't access to elements of a hash after getting them from a csv

我有一个 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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM