且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用ruby CSV模块导入csv文件时出现问题

更新时间:2022-10-31 13:00:23

好的,我错了两件事:

    $
  1. 导出的csv文件不应在字符串周围有引号。

    由于tokland, 。保存!是必要的(相对于我正在做的record.save) - 验证错误阻止创建记录。

最后,可以在创建模型/表Movie后创建以下函数:

  class Movie< ActiveRecord :: Base 
attr_accessible:media_format,:title,:copies_at_home,:order
require'csv'

def self.import_movies()
CSV.foreach 'public / uploads / movies.csv')do | row |
record = Movie.new(
:media_format => row [0],
:title => row [1],
:copies_at_home => row [2 ],
:order => row [3]

record.save!
end
end
end

以下:

  Blu-ray,电影1,1,1 
DVD,电影2,1,2
Blu-ray,Movie 3,1,3

  Movie.import_movies()

,并且如预期的那样,将在控制台中返回的所有内容将是:

  = > nil 

检查你的索引视图(如果你创建了一个),你应该看到记录已成功导入电影表。


I'm trying to use Ruby's csv module to import the records contained in a csv file to my local table in a Ruby on Rails 3 application.

The table was created through the creation of model Movie.

Here is what I've been executing in console:

require 'csv'
CSV.foreach('public/uploads/VideoTitles2.csv') do |row|
    record = Movie.new(
        :media_format   => row[0], 
        :title          => row[1], 
        :copies_at_home => row[2], 
        :order          => row[3]
    )
    record.save
end

The rows of the csv file match (in data type) the columns they're being passed into. Here is a shortened version of the csv file (VideoTitles2.csv) I'm attempting to import:

"DVD","LEAP OF FAITH",1,1
"DVD","COCOON",1,2
"DVD","TITANIC",1,3

where each record is separated by \n I believe. This csv file was exported from Access and its original file extension was .txt. I've manually changed it to .csv for sake of the import.

The problem is that, after executing the above lines in rails console, I get the following output:

=> nil

The import doesn't seem to happen. If anyone has an idea as to how I could remedy this I'd really appreciate it.

Okay there were two things I had wrong:

  1. The exported csv file should not have quotations around the strings - I just removed them.

  2. Thanks to tokland, the record.save! was necessary (as opposed to the record.save I was doing) - validation errors were preventing the records from being created.

So to conclude, one could just create the following function after creating the model/table Movie:

class Movie < ActiveRecord::Base
  attr_accessible :media_format, :title, :copies_at_home, :order
  require 'csv'

  def self.import_movies()
    CSV.foreach('public/uploads/movies.csv') do |row|
        record = Movie.new(
            :media_format   => row[0], 
            :title          => row[1], 
            :copies_at_home => row[2], 
            :order          => row[3]
        )
        record.save!  
    end
  end
end

Where movies.csv looks like the following:

Blu-ray, Movie 1, 1, 1
DVD, Movie 2, 1, 2
Blu-ray, Movie 3, 1, 3

then call this function in console as such:

Movie.import_movies()

and, as expected, all that would be returned in the console would be:

=> nil

Check your index view (if you've created one) and you should see that the records were successfully imported into the movies table.