Ruby on rails -从CSV文件创建哈希给出错误



我想创建一个散列,从一个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:in block in top (required)>'/home/anish/helios/dynalink/db/seeds.[font =宋体]>

谁能告诉我这里出了什么问题....提前感谢

change:

hash["hersteller"] = hersteller[hash["hersteller_nummer"]].strip.gsub("u00A0", "") if hash["hersteller_nummer"].present? #this is line number 54

:

hash["hersteller"] = hersteller[hash["hersteller_nummer"]].strip.gsub("u00A0", "") if hersteller[hash["hersteller_nummer"]].present?

你应该检查hersteller[hash["hersteller_nummer"]]的存在而不是hash["hersteller_nummer"]

相关内容

  • 没有找到相关文章

最新更新