使用Ruby CSV标题转换器 [英] Using Ruby CSV header converters
问题描述
说我有以下课程:
ActiveRecord :: Base
attr_accesible:first_name,:last_name
名字,姓氏
John,Doe
Jane,Doe
我想将CSV的内容保存到数据库中。我在Rake文件中有以下内容:
命名空间:migration do
desc迁移CSV数据
task:import,[:model,:file_path] => :environment do | t,args |
require'csv'
model = args.model.constantize
path = args.file_path
CSV.foreach(path,:headers => true,
:converters =>:all,
:header_converters => lambda {| h | h.downcase.gsub('','_')}
)do | row |
model.create!(row.to_hash)
end
end
end
我得到一个未定义的方法'downcase'为nil:NilClass
。如果我排除头转换器,那么我得到未知属性'名'
。将标题从名字
转换为 first_name
?
在我的桌面上做一些研究后,在我看来,错误是其他的。
首先,我将数据放入我的a.txt
文件中,如下所示:
名字,姓氏
John,Doe
Jane,Doe
$ b b
现在我运行的代码,它保存在我的 so.rb
文件。
so.rb
require'csv'
CSV.foreach :\\Users\\arup\\a.txt,
:headers => true,
:converters =>:all,
:header_converters = > lambda {| h | h.downcase.gsub('','_')}
)do | row |
p row
end
现在运行:
C:\Users\arup> ruby -v so.rb
ruby 1.9.3p448(2013-06-27)[i386- mingw32]
#< CSV :: Rowfirst_name:Johnlast_name:Doe>
#< CSV :: Rowfirst_name:Janelast_name:Doe>
所以一切都正在工作。现在让我重现一下错误:
我把数据放在我的a.txt
只是在最后一列之后添加了,
):
姓氏,
John,Doe
Jane,Doe
代码,它再次保存在我的 so.rb
文件中。
C:\Users\arup> ruby -v so.rb
ruby 1.9.3p448(2013-06-27)[i386-mingw32]
so.rb:5:in` < main>':undefined method`downcase'for nil:NilClass(NoMethodError)
在标题行中,存在导致错误的空白列值。因此,如果您有源 CSV 文件的控制,请检查相同。或者在代码中进行一些更改,以处理以下错误:
require'csv'
$ b b CSV.foreach(C:\\Users\\arup\\a.txt,
:headers => true,
:converters =>:all,
:header_converters => lambda {| h | h.downcase.gsub('','_')unless h.nil?}
)do | row |
p row
end
Say I have the following class:
class Buyer < ActiveRecord::Base attr_accesible :first_name, :last_name
and the following in a CSV file:
First Name,Last Name John,Doe Jane,Doe
I want to save the contents of the CSV into the database. I have the following in a Rake file:
namespace :migration do desc "Migrate CSV data" task :import, [:model, :file_path] => :environment do |t, args| require 'csv' model = args.model.constantize path = args.file_path CSV.foreach(path, :headers => true, :converters => :all, :header_converters => lambda { |h| h.downcase.gsub(' ', '_') } ) do |row| model.create!(row.to_hash) end end
end
I am getting an
undefined method 'downcase' for nil:NilClass
. If I exclude the header converters then I getunknown attribute 'First Name'
. What's the correct syntax for converting a header from, say,First Name
tofirst_name
?解决方案After doing some research here in my desktop, it seems to me the error is for something else.
First I put the data in my
"a.txt"
file as below :First Name,Last Name John,Doe Jane,Doe
Now I ran the code, which is saved in my
so.rb
file.so.rb
require 'csv' CSV.foreach("C:\\Users\\arup\\a.txt", :headers => true, :converters => :all, :header_converters => lambda { |h| h.downcase.gsub(' ', '_') } ) do |row| p row end
Now running the :
C:\Users\arup>ruby -v so.rb ruby 1.9.3p448 (2013-06-27) [i386-mingw32] #<CSV::Row "first_name":"John" "last_name":"Doe"> #<CSV::Row "first_name":"Jane" "last_name":"Doe">
So everything is working now. Now let me reproduce the error :
I put the data in my
"a.txt"
file as below ( just added a,
after the last column) :First Name,Last Name, John,Doe Jane,Doe
Now I ran the code, which is saved in my
so.rb
file, again.C:\Users\arup>ruby -v so.rb ruby 1.9.3p448 (2013-06-27) [i386-mingw32] so.rb:5:in `block in <main>': undefined method `downcase' for nil:NilClass (NoMethodError)
It seems, in your header row, there is blank column value which is causing the error. Thus if you have a control to the source CSV file, check there the same. Or do some change in your code, to handle the error as below :
require 'csv' CSV.foreach("C:\\Users\\arup\\a.txt", :headers => true, :converters => :all, :header_converters => lambda { |h| h.downcase.gsub(' ', '_') unless h.nil? } ) do |row| p row end
这篇关于使用Ruby CSV标题转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!