[英]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.