我似乎被困在试图访问来自csv文件的散列值,并且可以用另一双眼睛指出我犯的明显错误。以下是相关代码:
CSV.foreach(filename, headers: true, header_converters: :symbol, converters: :all) do |row|
data = row.to_hash
id = data['studentid'] # (have also tried id = data[':studentid'] but there are no :'s in the csv file headers, and double quotes instead of single)
title = data['title'] # also (title = data[:title'])
logger.debug "data param: #{data.inspect}"
logger.debug "data title param: #{title.inspect}"
logger.debug "data studentid param: #{id.inspect}"
从日志文件:(有效数据x'ed out或fake-note studentid以fixnum形式传入)
data param: {:lastname=>"XXXX", :firstname=>"XXXXX", :title=>"XXXXXXX", :studentid=>123456, :date=>"XXXXXXXXXX"}
data title param: nil
data studentid param: nil
rails 4.x ruby 2.x操作系统ubuntu
思想?
最佳答案
那些钥匙是符号。试用
id = data[:studentid]
title = data[:title]
注:不
data[':studentid']
。':studentid'
只是一个以:
开头的字符串