I use an iPhone application that periodically sends me an email in CSV format. I have a ruby ββscript that summarizes the data in this log with old logs. Recently, the application developer published an update that, for some unknown reason, added a carriage return to the end of each line, resulting in my script failing. According to the docs :row_end default :row_end should be :auto , which should accept either \r\n or \n (in 1.9.2). I tried using Ruby 1.8.7, 1.9.2 and FasterCSV with 1.8.7. I get various error messages with these different attempts, including
CSV::IllegalFormatError- Unquoted fields do not allow
\r or \n (line 1) ( FasterCSV::MalformedCSVError ) - cannot duplicate
NilClass (TypeError)
in 1.9.2. ( \r not in the field, this is the end of the line!) The data used to look like this:
03-12-2012 07:59,120.0, 03-11-2012 08:27,120.0, 03-10-2012 07:57,120.0,
Now it looks like this:
03-12-2012 07:59,120.0,^M 03-11-2012 08:27,120.0,^M 03-10-2012 07:57,120.0,^M
Thinking that CSV might think ^M is in the last field, I tried adding another comma:
03-12-2012 07:59,120.0,,^M
to no avail.
The only thing I can imagine is that CSV requires all fields to be in double quotes? I can come up with various workarounds, for example, first read the file, grind the ends, and then process the array using CSV, but first I want to find out what I'm doing wrong. It seems like it should work.
By the way, my code is just:
CSV.foreach(File.join($import_dir, file)) do |record|
and I tried installing :row_end => "\r\n" no avail.
I am on Mac OS X 10.6.8.