【发布时间】:2014-03-12 12:00:18
【问题描述】:
我想从一个包含大量数据的 CSV 文件创建一个哈希 我的 ruby 文件代码看起来像
hersteller = Hash[CSV.read("db/red.csv", col_sep: ',', row_sep: :auto, headers: true).map {|row| [row["lieferant_nr"], row["beschreibung"]]}] #this is line number 45
CSV.foreach("db/red.csv", col_sep: ',', row_sep: :auto, headers: true) do |row| # map keys
hash = Hash[row.map {|k, v| mapping[k] ? [mapping[k], v && v.strip.gsub("\u00A0", "")] : nil}.compact] # ignore NULL values
hash.reject! {|k, v| v == "NULL"} # get hersteller names
hash["hersteller"] = hersteller[hash["hersteller_nummer"]].strip.gsub("\u00A0", "") if hash["hersteller_nummer"].present? #this is line number 54
当我创建哈希时,这给了我一个错误
未定义的方法
strip' for nil:NilClass /home/anish/helios/dynalink/db/seeds.rb:54:inblock in top (required)>' /home/anish/helios/dynalink/db/seeds.rb:46:in top (required)>'
谁能告诉我这里出了什么问题...... 提前致谢
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-3 csv