Ruby 在Rails应用程序中运行Rake命令时出现错误消息

Ruby 在Rails应用程序中运行Rake命令时出现错误消息,ruby,ruby-on-rails-3,rake,Ruby,Ruby On Rails 3,Rake,我试图通过从CSV文件中提取数据在Rails应用程序中创建帖子 当我尝试运行Rake命令时,我得到下面的错误消息。这个代码怎么了 SEED.RAKE文件 require 'csv' namespace :csv do desc "Import CSV Data for Michelin Star Restaurants" task :post => :environment do csv_file_path = 'db/data.csv' CSV.forea

我试图通过从CSV文件中提取数据在Rails应用程序中创建帖子

当我尝试运行Rake命令时,我得到下面的错误消息。这个代码怎么了

SEED.RAKE文件

require 'csv'

namespace :csv do

  desc "Import CSV Data for Michelin Star Restaurants"
  task :post => :environment do

    csv_file_path = 'db/data.csv'

    CSV.foreach(csv_file_path) do |row|
        Post.create({
          :name => row[1],
          :address => row[2],
          :city => row[3],
          :michelin_status => row[4],
          :website => row[5],
          :phone => row[6],
          :longitude => row[7],
          :latitude => row[8],
          :inthenews => row[9],
          :google_reviews => row[10],
        })
      end
    end
  end
来自控制台的错误消息

Faisals-Air:dinner fkhalid2008$ rake csv:post
rake aborted!
ArgumentError: invalid byte sequence in UTF-8
/Users/fkhalid2008/dinner/lib/tasks/seed.rake:11:in `block (2 levels) in <top (required)>'
/Users/fkhalid2008/.rvm/gems/ruby-2.2.0/bin/ruby_executable_hooks:15:in `eval'
/Users/fkhalid2008/.rvm/gems/ruby-2.2.0/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => csv:post
(See full trace by running task with --trace)
class CreatePosts < ActiveRecord::Migration
  def change
    create_table :posts do |t|
      t.string :name
      t.string :inthenews
      t.string :michelin_status
      t.string :google_reviews
      t.string :address
      t.string :city
      t.string :website
      t.integer :phone
      t.integer :longitude
      t.integer :latitude

      t.timestamps null: false
    end
  end
end
费萨尔航空公司:晚餐fkhalid2008$rake csv:post 雷克流产了! ArgumentError:UTF-8中的字节序列无效 /Users/fkhalid2008/dinner/lib/tasks/seed.rake:11:in'block(2层)in' /Users/fkhalid2008/.rvm/gems/ruby-2.2.0/bin/ruby\u executable\u hooks:15:in'eval' /Users/fkhalid2008/.rvm/gems/ruby-2.2.0/bin/ruby\u executable\u hooks:15:in`' 任务:TOP=>csv:post (通过使用--trace运行任务查看完整跟踪) DB迁移文件

Faisals-Air:dinner fkhalid2008$ rake csv:post
rake aborted!
ArgumentError: invalid byte sequence in UTF-8
/Users/fkhalid2008/dinner/lib/tasks/seed.rake:11:in `block (2 levels) in <top (required)>'
/Users/fkhalid2008/.rvm/gems/ruby-2.2.0/bin/ruby_executable_hooks:15:in `eval'
/Users/fkhalid2008/.rvm/gems/ruby-2.2.0/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => csv:post
(See full trace by running task with --trace)
class CreatePosts < ActiveRecord::Migration
  def change
    create_table :posts do |t|
      t.string :name
      t.string :inthenews
      t.string :michelin_status
      t.string :google_reviews
      t.string :address
      t.string :city
      t.string :website
      t.integer :phone
      t.integer :longitude
      t.integer :latitude

      t.timestamps null: false
    end
  end
end
class CreatePosts
CSV文件屏幕截图

这是因为文件中的某些地方有一些无效字节

要避免此问题,可以使用以下方法:

CSV.foreach(csv_file_path) do |row|
  Post.create({
                  :name => row[1].scrub,
                  :address => row[2].scrub,
                  :city => row[3].scrub,
                  :michelin_status => row[4].scrub,
                  :website => row[5].scrub,
                  :phone => row[6].scrub,
                  :longitude => row[7].scrub,
                  :latitude => row[8].scrub,
                  :inthenews => row[9].scrub,
                  :google_reviews => row[10].scrub,
              })
end
CSV.foreach(csv_file_path, "r:ISO-8859-1") do |row|
 . . . 
 . . . 
end
更新 读取CSV文件时,请尝试指定
编码类型,如下所示:

CSV.foreach(csv_file_path) do |row|
  Post.create({
                  :name => row[1].scrub,
                  :address => row[2].scrub,
                  :city => row[3].scrub,
                  :michelin_status => row[4].scrub,
                  :website => row[5].scrub,
                  :phone => row[6].scrub,
                  :longitude => row[7].scrub,
                  :latitude => row[8].scrub,
                  :inthenews => row[9].scrub,
                  :google_reviews => row[10].scrub,
              })
end
CSV.foreach(csv_file_path, "r:ISO-8859-1") do |row|
 . . . 
 . . . 
end

您只需使用
nkf
将原始csv文件转换为utf8即可

$ nkf -w --overwrite db/data.csv

不起作用。我收到一条错误消息,说'scrub'是一个未定义的方法。我还收到.mb_charsLet us的未定义方法。