How do I import using FasterCSV a row with a name like Ciarán?
I've read elsewhere that this can be fixed by setting KCODE. For example:
$KCODE = "U"
Stick this at the top.
James Edward Gray has also said he's added encoding support to FasterCSV but it's in trunk only.
Trying to do a CSV to multiple tables import with FasterCSV
Try something like:
associated_category = Category.find_by_name(row['Category'])
Produkt.create(
:category_id => associated_category.id,
:name => row[2]
)
I do it that way: http://gist.github.com/324613
Harm
FasterCSV tutorial to import data to database?
So it looks like FasterCSV is now part of the Ruby core as of Ruby 1.9, so this is what I ended up doing, to achieve the goals in my question above:
@importedfile = Import.find(params[:id])
filename = @importedfile.csv.path
CSV.foreach(filename, {:headers => true}) do |row|
@post = Post.find_or_create_by_email(
:content => row[0],
:name => row[1],
:blog_url => row[2],
:email => row[3]
)
end
flash[:notice] = "New posts were successfully processed."
redirect_to posts_path
Inside the find_or_create_by_email
function is the mapping from the database columns to the columns of the CSV file: row[0], row[1], row[2], row[3]
.
Since it is a find_or_create
function I don't need to explicitly call @post.save
to save the entry to the database.
If there's a better way please update or add your own answer.
Having issues importing a CSV File using FasterCSV
Issue is resolved thanks to James Gray
def csv_import
file = params[:file]
FCSV.new(file.tempfile, :headers => true).each do |row|
Script.create!(:name => row[0],
:task => row[1],
:expected_results => row[2],
:require_id => row[3],
:department_id => 1,
:category_id => 1)
end
end
Headers on the second row in FasterCSV?
According to the docs, fTable = FasterCSV.table("sto.csv", :return_headers => false)
should do what you want. .table
implies :headers => true
The docs have this info.
Best way to read CSV in Ruby. FasterCSV?
Ruby 1.9 adopted FasterCSV as its core CSV processor, so I would say it's definitely better to go for FasterCSV, even if you're still using Ruby 1.8
Ruby on Rails Moving from CSV to FasterCSV
CSV::Reader.parse(File.open('file.csv')){|row| puts row}
or
CSV::Reader.parse("some, content\nanother, content"){|row| puts row}
and
FasterCSV.parse(File.open('file.csv')){|row| puts row}
or
FasterCSV.parse("some, content\nanother, content"){|row| puts row}
are equivalent.
But
FasterCSV.read('filename')
takes filename as parameter and reads and parse data from the file however you are dumping the file content as you are passing data in the parameter
@parsed_file = FasterCSV.parse(params[:dump][:file])
@parsed_file.each do |row|
puts row
# and do some operations
end
should work fine.
FasterCSV: columns into an array -- rails
Thanks EmFi, with your help I was able to come up with a solution.
This takes a remote url csv file and loads it into a multi-dimensional array, based on columns.
require 'rio'
require 'fastercsv'
url = 'http://remoteurl.com/file.csv'
people = FasterCSV.parse(rio(url).read)
Related Topics
Using Ruby 2.0 on Amazon Opsworks
Rails 5, "Nil Is Not a Valid Asset Source"
How to Use Ruby to Write Individual Records to a Redshift Database
How to Pass Named Arguments to a Rake Task
Can You Create/Write/Append a String to a File in a Single Line in Ruby
Rounding a Float to the Nearest Integer in Ruby
Equivalent Ruby .Times in Coffeescript
Access Translation File (I18N) from Inside Rails Model
Error Nomethoderror: Undefined Method 'Debug_Rjs=' for Actionview::Base:Class
Error Connecting to Redis on 127.0.0.1:6379 (Errno::Econnrefused) - Wercker
Carrierwave Cannot Remove Image
Count Records Created Within the Last 7 Days
How to Profile Rspec with Perftools and Bundler