Unquoted fields do not allow \r or \n Error
Similar to the solution found here Rails FasterCSV "unquoted fields do not allow \r or \n" I copied and pasted the information into a new csv file not making any changes to the format and the seed program worked.
CSV - Unquoted fields do not allow \r or \n (line 2)
First of all, you should set you column delimiters to ';', since that is not the normal way CSV files are parsed. This worked for me:
CSV.open('file.csv', :row_sep => :auto, :col_sep => ";") do |csv|
csv.each { |a,b,c| puts "#{a},#{b},#{c}" }
end
From the 1.9.2 CSV documentation:
Auto-discovery reads ahead in the data looking for the next
\r\n
,
\n
, or\r
sequence. A sequence will be selected even if it occurs
in a quoted field, assuming that you would have the same line endings
there.
Rails FasterCSV unquoted fields do not allow \r or \n
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.
CSV parsing returns Unquoted fields do not allow \r or \n but can't find error in source file?
There are inconsistent line endings in that text, and the CSV parser is stumbling over them. A very quick fix is to remove all \r
characters with:
response.body.gsub!("\r", '')
In case you are curious, one way to see the errant characters is the following code that writes the Ruby array notation of every character to a text file:
response = HTTParty.get("http://" + "weather.com/ads.txt", limit: 100, follow_redirects: true, timeout: 10)
characters = response.chars.inspect
output = File.open( "outputfile.txt","w" )
output << characters
output.close
Open up outputfile.txt
and search for \r
characters. I find just a couple of them at line endings, though all other lines are ended with \n
alone.
CSV::MalformedCSVError: Unquoted fields do not allow \r or \n in Ruby
I fixed the issue by using the following:
format_csv = replace_empty_string.gsub(/\r\r?\n?/, "\n")
This was originally @mgrims answer, but I had to adjust my code by further removing the :skip_blanks :row_sep
options.
It is importing successfully now!
Ruby 1.9.3. CSV - Unquoted fields do not allow \r or \n
You may need to specify the encoding when opening the file. Try using something like this until you manage to decode the file:
File.open(file.path, "rb:UTF-16BE").read.encode("utf-8")
The encoding of your file seems to be UTF-16, so try UTF-16, UTF-16LE and UTF-16BE.
After that you can try to feed the encoded data into a CSV reader like this:
CSV.open(File.open(file.path, "rb:UTF-16BE")) do |csv|
and process the file. You may need to re-encode the data into UTF-8 at some point. It all depends on your use case.
With Ruby, how to import a CSV file that has a row that is missing a value in the last column, and thus ends with a comma?
Your line endings are probably not \r\n
then.
You can try using :row_sep => :auto
(it'll look for different EOLs).
If your CSV file is not supposed to contain fields with multiple lines I'll advice you to clean the whole thing by e.g. file.gsub(/\r\r?\n?/, "\n")
, and use a simple "\n"
for row_sep.
See https://stackoverflow.com/a/18969935/4640187
Ruby CSV does not understand \r\n as row end
Works for me in 1.9.3:
mark@ubuntu:~$ irb
1.9.3p0 :001 > require 'csv'
=> true
1.9.3p0 :002 > CSV.foreach("rn.csv") do |row|
1.9.3p0 :003 > p row
1.9.3p0 :004 > end
["1","2","3","4","5"]
["6","7","8","9","10"]
And the file does indeed have carriage returns in it:
mark@ubuntu:~$ od -a rn.csv
0000000 1 , 2 , 3 , 4 , 5 cr nl 6 , 7 , 8
0000020 , 9 , 1 0 cr nl
0000027
Related Topics
What Does ["String"].Pack('H*') Mean
How to Split a String by Commas Except Inside Parenthesis, Using a Regular Expression
Rails 3 Order by Count on Has_Many :Through
Ruby on Rails 4 - Simple_Form Multiple Select Input
How to Use Functions Like Concat(), etc. in Arel
How to Reference a Constant in a Yaml with Rails
How to Strip Leading and Trailing Quote from String, in Ruby
Error Installing SQLite3 Gem via Bundler
Rspec: Should Be (This or That)
How to Stringize/Serialize Ruby Code
How to Get a Selenium/Ruby Bot to Wait Before Performing an Action