2014-03-12 7 views
0

Я хочу, чтобы создать хэш, из файла CSV, который имеет кучу данных мой код рубин файл выглядит каксоздать хэш из CSV-файла дает ошибку

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 блока в верхней части (требуется)> ' /home/anish/helios/dynalink/db/seeds.rb:46:in сверху (обязательно)>'

Может кто-нибудь предложить мне, что пошло не так здесь .... Заранее спасибо

ответ

0

изменение:

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"]

Смежные вопросы