Search code examples
javacsvopencsv

Importing only specific columns from CSV file


I'm trying to import CSV files using Java library openCSV. Is there any way to load only specific columns not all of them? My code right now is:

JFileChooser fileopen = new JFileChooser();
                FileFilter filter = new FileNameExtensionFilter(
                        "CSV file", "csv");
                fileopen.addChoosableFileFilter(filter);

                int ret = fileopen.showDialog(null, "Choose file");
                                if (ret == JFileChooser.APPROVE_OPTION) {
    try {
        File file = fileopen.getSelectedFile();
        System.out.println("Opening: " + file.getName());
        File csvFilename = fileopen.getSelectedFile();
        try (CSVReader csvReader = new CSVReader(new FileReader(csvFilename), ',', '\'',11 )) {
            String[] row = null;
            while((row = csvReader.readNext()) != null) {
                System.out.println(row[0]
                        + " # " + row[1]
                        + " #  " + row[2]);
            }
        } 
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Glass_search.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Glass_search.class.getName()).log(Level.SEVERE, null, ex);
    }

    }

This file also got headings so maybe this way?


Solution

  • For that particular case univocity-parsers is a much better fit as you can easily choose what columns to get:

    CsvParserSettings parserSettings = new CsvParserSettings();
    settings.detectFormatAutomatically(); //detects the format 
    
    //extracts the headers from the input
    settings.setHeaderExtractionEnabled(true);
    
    //or give the header names yourself
    // if you use this it will override the headers read from the input (enabled above).
    settings.setHeaders("A", "B", "C");
    
    //now for the column selection
    settings.selectFields("A", "C"); //rows will contain only values of column "A" and "C"
    //or
    settings.selectIndexes(0, 2); //rows will contain only values of columns at position 0 and 2
    
    List<String[]> rows = new CsvParser(settings).parseAll(new File("/path/to/your.csv"), "UTF-8");
    

    You can also get the values of each column in a List:

    ColumnProcessor columnProcessor = new ColumnProcessor();
    settings.setProcessor(columnProcessor);
    CsvParser parser = new CsvParser(settings);
    parser.parse(new File("/path/to/your.csv), "UTF-8"); //all rows are submitted to the processor created above.
    
    Map<String, List<String>> columnValues = columnProcessor.getColumnValuesAsMapOfNames();
    

    This should solve all your problems, hope it helps.

    Disclaimer: I'm the author of this library. It's open-source and free (Apache 2.0 license)