1

I have this csv structure

"CATEGORY";"NAME";"AGE"
"Red";"John";"34"

When I import the file through smarter_csv gem I get this hash

{:"\"CATEGORY\""=>"\"Red\"", :"\"NAME\""=>"\"John\"", :"\"AGE\""=>"\"34\""}

The code I use is the following

options = {:col_sep => ";",:row_sep => :auto, :quote_char => "\x00"}
SmarterCSV.process(save_folder, options) do |array|
    Item.create(array.first)
end

What puzzles me is the \" that is added in each item of the hash. I have used this same method before without issues and I don't understand what is going wrong, but the expected hash should be plan text with no backslash and additional quotes. As a note, is I don't use the ";quote_char => "\x00"" option I get a malformed csv error.

Jack
  • 497
  • 5
  • 16

1 Answers1

0

This works out of the box, but the quote_char you chose was incorrect; it should be '"', which is the default setting.

      require 'smarter_csv'
      SmarterCSV::VERSION
       => "1.2.3"

      data = SmarterCSV.process('/tmp/test.csv', {:col_sep => ";"})
       => [{:category=>"Red", :name=>"John", :age=>34}]

      # or like this:

      data = SmarterCSV.process('/tmp/test.csv', {
         :col_sep => ";",:row_sep => :auto, :quote_char => '"'
      })
       => [{:category=>"Red", :name=>"John", :age=>34}]          
Tilo
  • 33,354
  • 5
  • 79
  • 106