Search code examples
csvopencsv

Can OpenCSV ignore trailing commas on records?


A CSV with trailing commas like this:

name, phone
joe, 123-456-7890,
bob, 333-555-6666,

processed like this:

CSVReaderHeaderAware r = new CSVReaderHeaderAware(reader);
Map<String, String> values = r.readMap();

will throw this exception:

java.io.IOException: Error on record number 2: The number of data elements is not the same as the number of header elements

For now I'm stripping commas from input files using sed:

find . -type f -exec sed -i 's/,*\r*$//' {} \;

Is there some easy way to tell OpenCSV to ignore trailing commas?


Solution

  • OpenCSV maintainers commented here. As of OpenCSV v5.1 there is no simple way to accomplish this and pre-processing the file using sed, etc is best for now.