2

I have a client who wants to maintain a broad span of flexibility when submitting CSV files to be uploaded. I've been using SmarterCSV to process the files, but have hit a snag where there are gaps in the columns of information--essentially the header line looks something like this:

'header1',,,,,'header2','header3'

SmarterCSV gives me this error:

SmarterCSV::DuplicateHeaders (ERROR: duplicate headers: ,,,,,,,,,)

Does anyone know of a way to have SmarterCSV just skip columns without headers and move on? I've tried something akin to the following with no luck:

options = {strip_whitespace: true, key_mapping: {nil => nil}
records = SmarterCSV.process("#{path}#{csv_file}", options)

EDIT:

I've also tried this, and it allows me to complete the processing, but results in an empty array:

options = {
      strip_whitespace: true, 
      key_mapping: {'' => nil},
      remove_unmapped_keys: true
    }
    records = SmarterCSV.process("#{path}#{csv_file}", options)

1 Answers1

0

I think you should make two changes:

  1. set the key_mapping: to include all the keys you expect
  2. remove '' => nil from the key mapping. eg
options = {
  strip_whitespace: true, # you can remove this since it's the default
  key_mapping: { column1: :column1, column2: :column2 },
  remove_unmapped_keys: true
}

Now unmapped keys, such as empty ones, will be removed.

hirowatari
  • 3,195
  • 2
  • 14
  • 15