Search code examples
javacsvunivocity

uniVocity parser to handle duplicate header names


How can I read a csv file which has duplicate column names by using BeanParser.

Below is the example header

Col desc, Col amount, Col desc, Col amount

test, 12.44, test2, 43.44


Solution

  • You can set the header names manually:

    public class MyClass {
    
        @Parsed(field = "amount1")
        private BigDecimal amount1;
    
        @Parsed(field = "amount2")
        private BigDecimal amount2;
    
        @Parsed(field = "description1")
        private String description1;
    
        @Parsed(field = "description2")
        private BigDecimal description2;
    }
    

    Then:

    CsvParserSettings parserSettings = new CsvParserSettings();
    parserSettings.setHeaderExtractionEnabled(true); //to read your input headers with duplicates.
    parserSettings.setHeaders("description1", "amount1, "description2", "amount2"); //to override the headers in the input with the names you want to work with
    

    Then parse:

    List<MyClass> myList = new CsvRoutines(parserSettings).parseAll(MyClass.class, <input>);
    

    You can also use the field index instead of the field names in the annotations, so you don't need to worry about header names.

    Hope this helps