Search code examples
javacsvopencsvsupercsv

How do I skip white-space only lines and lines having variable columns using supercsv


I am working on CSV parser requirement and I am using supercsv parser library. My CSV file can have 25 columns(separated by tab(|)) and up to 100k rows with additional header row.

I would like to ignore white-space only lines and lines containing less than 25 columns.

I am using IcvBeanReader with name mappings(to set csv values to pojo) and field processors(to handle validations) for reading a file.

I am assuming that Supercsv IcvBeanReader will skip white space lines by default. But how to handle if a row contains less than 25 column numbers?


Solution

  • You can easily do this by writing your own Tokenizer.

    For example, the following Tokenizer will have the same behaviour as the default one, but will skip over any lines that don't have the correct number of columns.

    public class SkipBadColumnCountTokenizer extends Tokenizer {
    
        private final int expectedColumns;
    
        private final List<Integer> ignoredLines = new ArrayList<>();
    
        public SkipBadColumnCountTokenizer(Reader reader, 
                CsvPreference preferences, int expectedColumns) {
            super(reader, preferences);
            this.expectedColumns = expectedColumns;
        }
    
        @Override
        public boolean readColumns(List<String> columns) throws IOException {
            boolean moreInputExists;
            while ((moreInputExists = super.readColumns(columns)) && 
                columns.size() != this.expectedColumns){
                System.out.println(String.format("Ignoring line %s with %d columns: %s", getLineNumber(), columns.size(), getUntokenizedRow()));
                ignoredLines.add(getLineNumber());
            }
    
            return moreInputExists;
    
        }
    
        public List<Integer> getIgnoredLines(){
            return this.ignoredLines;
        }
    }
    

    And a simple test using this Tokenizer...

    @Test
    public void testInvalidRows() throws IOException {
    
        String input = "column1,column2,column3\n" +
                "has,three,columns\n" +
                "only,two\n" +
                "one\n" +
                "three,columns,again\n" +
                "one,too,many,columns";
    
        CsvPreference preference = CsvPreference.EXCEL_PREFERENCE;
        int expectedColumns = 3;
        SkipBadColumnCountTokenizer tokenizer = new SkipBadColumnCountTokenizer(
            new StringReader(input), preference, expectedColumns);
    
        try (ICsvBeanReader beanReader = new CsvBeanReader(tokenizer, preference)) {
            String[] header = beanReader.getHeader(true);
            TestBean bean;
            while ((bean = beanReader.read(TestBean.class, header)) != null){
                System.out.println(bean);
            }
            System.out.println(String.format("Ignored lines: %s", tokenizer.getIgnoredLines()));
        }
    
    }
    

    Prints the following output (notice how it's skipped all of the invalid rows):

    TestBean{column1='has', column2='three', column3='columns'}
    Ignoring line 3 with 2 columns: only,two
    Ignoring line 4 with 1 columns: one
    TestBean{column1='three', column2='columns', column3='again'}
    Ignoring line 6 with 4 columns: one,too,many,columns
    Ignored lines: [3, 4, 6]