Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails FasterCSV "unquoted fields do not allow \r or \n"

I'm having an issue with FasterCSV and my rake db:seeds migration. I get the error: "rake aborted! Unquoted fields do not allow \r or \n (line 2)" on the following seeds.rb data:

require 'csv' 

directory = "db/init_data/"

file_name = "gardenzing020812.csv"
path_to_file = directory + file_name
puts 'Loading Plant records'
# Pre-load all Plant records
n=0
CSV.foreach(path_to_file) do |row|
  Plant.create! :name => row[1],
  :plant_type => row[3],
  :group => row[2],
  :image_path => row[45],
  :height => row[5],
  :sow_inside_outside => row[8]
n=n+1
end                 

I've searched for a solution to this problem and have discovered that for a lot of folks it's a UTF-8 encoding problem. I've tried requiring iconv and :encoding => 'u', but that then gives me the error "invalid byte sequence in UTF-8".

I'm a newbie, and I can't figure out if it's really an encoding issue that I need to crack (which I've been trying to do unsuccessfully and if so, I could really use some guidance) or, more likely I feel, that I've made a simple misstep and done something wrong with the way I've set up seeds.rb and possibly my excel -> csv file. There's no bad or awkward data in the csv file. It's simple one-word strings, text and integers. Please help!

like image 554
Matthew Melone Avatar asked Feb 08 '12 19:02

Matthew Melone


2 Answers

It was as simple as clearing all the formatting off in the csv. Excel seems to have a habit of retaining a lot of the formatting after saving in a csv file, which was causing the failure. After I copied and pasted all the data with no formatting in a new csv file, it was fine.

like image 163
Matthew Melone Avatar answered Nov 03 '22 01:11

Matthew Melone


Use String.encode(universal_newline: true) instead gsub. It converting CRLF and CR to LF # Always break lines with \n

like image 28
Vaisakh VM Avatar answered Nov 03 '22 00:11

Vaisakh VM