2

Using CsvMapWriter if I add a header say, 'Region' and later write a row with key 'region' and value 'Northeast' it doesn't get added to the SuperCSV output. I'm assuming it doesn't find a valid column to put the value for 'region' in.

For this application I can't rename the headers, such as forcing lowercase. And the row key/value pairs, like 'region' and 'northeast' could come in as upper, lower or mixed case, but regardless it should be added to the proper case-insensitive header column (the header itself may be 'Region', 'REGION' etc)

xref
  • 1,707
  • 5
  • 19
  • 41

1 Answers1

1

Two solutions come to mind.

  1. Use CsvBeanWriter instead of CsvMapWriter, if possible.

    - or -

  2. Wrap your Map with a lowercasing Map implementation (or copy the values over to a new map with the correct casing).

A lowercasing map implementation (org.apache.commons.collections.map.CaseInsensitiveMap) is available from Apache Commons Collections.

Trevor Freeman
  • 7,112
  • 2
  • 21
  • 40
  • 1
    I'm not sure I can use BeanWriter in this case, as the csv is a selection of fields from about 7 different classes? – xref Aug 01 '13 at 22:00
  • @xref You could still add a custom class that contains all values, which you add to instead of the Map. Worst case though, reading through all the entries of your Map and putting them into a new Map with all lowercase keys would not be too difficult (not terribly efficient, but probably not a huge issue unless you are processing upwards of 100,000 rows). – Trevor Freeman Aug 01 '13 at 22:24
  • Well I don't know in advance what the names of the headers will be, or how many there will be, as every csv is unique. Could a custom class handle that in supercsv? – xref Aug 05 '13 at 16:22
  • @xref If you do not know the names of the headers then I would suggest simply remapping the values to a new map with all lowercase keys, or using a map implementation that does this automatically (E.g. `org.apache.commons.collections.map.CaseInsensitiveMap` from the Apache Commons library). – Trevor Freeman Aug 06 '13 at 17:10