I have a cvs and I want to access the values. If I do this:
require 'csv'
CSV.foreach(filename, headers: true) do |row|
new_row = row.to_hash
puts "#{new_row}"
end
This is the result -> {"user_id"=>"111", "sport"=>"aaa"}
But if I do this nothing gets printed
require 'csv'
CSV.foreach(filename, headers: true) do |row|
new_row = row.to_hash
puts "#{new_row["user_id"]}"
end
Why is this? How can I get the info?
Try using a symbol to find the key instead of a string
require 'csv'
CSV.foreach(filename, headers: true) do |row|
new_row = row.to_hash
puts new_row[:user_id]
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.