0

类似于这个问题...

导入 CSV 时,我收到以下 Ruby 1.9.3 错误:“TypeError: can't convert String into Integer”

全文如下:

pry(main)> Lightbulb.import
TypeError: can't convert String into Integer
from /Users/user/.rvm/gems/ruby-1.9.3-p448/bundler/gems/rails-1c2717d3f5a3/activesupport/lib/active_support/core_ext/object/try.rb:36:in `[]'

我相信这是因为我们将字符串 (Amazon ASIN) 作为索引传递给 lightbulb.rb 中的数组,以及从 Amazon 产品库存中导入其他数据。

它适用于除我的 35 行之外的所有行。我认为这是因为这个特定亚马逊产品的数据无法转换为整数?我真的不知道...

这是导入过程失败,在 CSV(下面列出)中有一行无法将字符串转换为整数:

  # asin  organization_name lumens_per_watt light_output  watts life_hours  light_color
  def self.import
    destroy_all
    table = CSV.read("#{Rails.root}/vendor/data/bulb.csv", :headers => true)
    table.each do |row|
      ap = Product.find_or_save(row['asin'])
      create(
        :price => ap.lowest_new_price,
        :small_image => ap.small_image,
        :medium_image => ap.medium_image,
        :large_image => ap.large_image,
        :detail_page_url => ap.detail_page_url,
        :asin => ap.asin,
        :brand => row['organization_name'],
        :feature => ap.feature,
        :organization_name => row['organization_name'],
        :label => ap.label,
        :manufacturer => row['organization_name'],
        :product_model => ap.product_model,
        :sku => ap.sku,
        :title => ap.title,
        :total_new => ap.total_new,
        :editorial_reviews => ap.editorial_reviews,
        :efficiency => row['lumens_per_watt'],
        :brightness => row['light_output'],
        :actual_watts => row['watts'],
        :expected_life => row['life_hours'],
        :light_color => row['light_color'],
        :perceived_watts => calculate_perceived_watts(row['light_output'])
      )
    end
  end

这是 Product.find_or_save 的完整代码:

class Product
  include Mongoid::Document
  include Mongoid::Timestamps

  field :asin, type: String
  field :parent_asin, type: String
  field :detail_page_url, type: String
  field :item_links, type: Array, default: []
  field :sales_rank, type: String
  field :small_image, type: String
  field :medium_image, type: String
  field :large_image, type: String
  field :image_sets, type: String, default: []
  field :brand, type: String
  field :ean, type: String
  field :ean_list, type: Hash, default: {}
  field :feature, type: Array, default: []
  field :item_dimensions, type: Hash, default: {}
  field :label, type: String
  field :manufacturer, type: String
  field :product_model, type: String
  field :mpn, type: String
  field :package_dimensions, type: Hash, default: {}
  field :part_number, type: String
  field :product_group, type: String
  field :product_type_name, type: String
  field :publisher, type: String
  field :sku, type: String
  field :studio, type: String
  field :title, type: String
  field :lowest_new_price, type: String
  field :lowest_used_price, type: String
  field :total_new, type: String
  field :total_used, type: String
  field :offers, type: Hash, default: {}
  field :customer_reviews, type: String
  field :editorial_reviews, type: String
  field :similar_products, type: Array, default: []
  field :nodes, type: Array, default: []

  def self.find_or_save(asin)
    if item = where(asin: asin).first
      item
    else
      amazon_product_hash = AmazonApi.product(asin)
      attrs = ProductWrapper.parse(amazon_product_hash)
      create(attrs)
    end
  end

如果我有一排失败 - 只有一排!:)

B00B4CPKT4,Philips,56,730,13,25000,2700

如果我删除该行,仅 35 行中的 1 行,它将完美运行。它总是在 .csv 中的那一行失败。我把它放在第一位、中间和最后——甚至重写它以找到任何隐藏的 gremlin 字符。

例如,这是标题和第一行,它们完美无缺:

asin,organization_name,lumens_per_watt,light_output,watts,life_hours,light_color
B00BXG7UZ8,Cree,75,450,6,25000,2700

我尝试添加.to_i:asin => ap.asin.to_i,但没有运气!

这是我在 development.log 中看到的最后一行,我看到该行的 13 个实例对应于我尝试在bulb.csv 文件中使用这个有缺陷的产品运行 Lightbulb.import 的 13 次令人沮丧的时间:

  MOPED: 54.234.253.6:33037 QUERY        database=heroku_app14604604 collection=products selector={"$query"=>{"asin"=>"B00B4CPKT4"}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil (5231.9772ms)
4

0 回答 0