Search code examples
rubycsvsmartercsv

Ruby - CSV works while SmarteCSV doesn't


I want to open a csv file using SmarterCSV.process

  market_csv = SmarterCSV.process(market)
  p "just read #{market_csv}"

The problem is that the data is not read and this prints:

[]

However, if I attempt the same thing with the default CSV library implementation the content of the file is read(the following print statement prints the file).

CSV.foreach(market) do |row|
    p row
end

The content of the file I was reading is of the form:

Date,Close
03/06/15,0.1634
02/06/15,0.1637
01/06/15,0.1638
31/05/15,0.1638

Solution

  • The problem could come from the line separator, the file is not exactly the same if you're using windows or unix system ("\r\n" or "\r"). Try to identify and specify the character in the SmarterCSV.process like this:

    market_csv = SmarterCSV.process(market, row_sep: "\r")
    p "just read #{market_csv}"
    

    or like this:

    market_csv = SmarterCSV.process(market, row_sep: :auto)
    p "just read #{market_csv}"