使用ruby CSV模块导入csv文件时出现问题 [英] Trouble importing csv file with ruby CSV Module

查看:210
本文介绍了使用ruby CSV模块导入csv文件时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Ruby的csv模块将包含在csv文件中的记录导入到Ruby on Rails 3应用程序中的本地表。





以下是我在控制台中执行的操作:

  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

csv文件的行与(传入数据类型)的列匹配。这是一个缩写版本的csv文件(VideoTitles2.csv)我试图导入:

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

其中每个记录由\\\
分隔。此csv文件从Access导出,其原始文件扩展名为.txt。我手动将它改为.csv为了导入。



问题是,在rails控制台中执行上面的行后,我得到以下输出:

  => nil 

导入似乎不会发生。如果任何人有一个想法,如何我可以补救这一点,我真的很感激。

解决方案

好的,我错了两件事:

    $


  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.

这篇关于使用ruby CSV模块导入csv文件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆