3

Also, allows handling of long rows in a readable/easy manner, in writing them as well as in reading them.

I've looked into opencsv but it does not refer to elements in row in any sequential fashion, and supercsv does not allow looking into column by name but number only.

Another caveat - for which I need to be able to address columns by name - is that the number of columns is not constant, and only part of the column headers are constant, something like:

Name|Number|Color1|Color2......|Color67|   

where not all colors between 1 and 67 are present in any given CSV file.

bnjmn
  • 4,508
  • 4
  • 37
  • 52
akapulko2020
  • 1,079
  • 2
  • 22
  • 36
  • Does this similar question http://stackoverflow.com/questions/200609/can-you-recommend-a-java-library-for-reading-and-possibly-writing-csv-files help? – andyb Jul 18 '11 at 14:42
  • not exactly, as I've already found it and read the answers, but trying again now... – akapulko2020 Jul 18 '11 at 14:50

3 Answers3

6

I can't really speak to opencsv, but if you have a fixed number of fields you might be able to use the JavaBean binding to do something like this.

Super CSV however, definitely supports reading and writing CSV rows by name using CsvBeanReader, CsvDozerBeanReader or CsvMapReader (and their writing equivalents).

If you prefer to keep things simple and use a map (with the column name as key, and the column value as the value) then you could use CsvMapReader. There are examples on reading and writing with CsvMapReader on the Super CSV website.

A reading example:

 ICsvMapReader mapReader = new CsvMapReader(
      new FileReader("foo.csv"), CsvPreference.EXCEL_PREFERENCE);
 try {
  final String[] headers = mapReader.getHeader(true);
  Map<String, String> row;
  while( (row = mapReader.read(headers)) != null) {
    for (String header : headers) {
      System.out.println(header + " is " + row.get(header));
    }
  }
} finally {
  mapReader.close();
}

Writing is quite similar.

James Bassett
  • 9,458
  • 4
  • 35
  • 68
ig0774
  • 39,669
  • 3
  • 55
  • 57
  • 1
    @akapulko: That's not an issue with the CsvMapReader, which can handle an arbitrary number and arrangement of columns. – ig0774 Jul 19 '11 at 11:37
  • thanks for the code sample and the links, going to deep-dive into them :) – akapulko2020 Jul 20 '11 at 08:07
  • i know this is old, but `inFile` in this example should be `mapReader` and `row.get("header")` should be `row.get(header)` - thanks anyways for this example :) – Stefan Mar 03 '12 at 19:52
3

You could use the open source library uniVocity-parsers. With this library, not only you can select columns as you want, but also you can select columns in any sequence.

With the following headers: Name|Number|Color1|Color2......|Color67|

We choose only the columns "Color1", "Color3", and "Color2" from the csv with the following code:

public static void main(String[] args) throws FileNotFoundException {

    // 1st, config the CSV reader
    CsvParserSettings settings = new CsvParserSettings();
    settings.getFormat().setLineSeparator("\n");
    settings.selectFields("Color1", "Color3", "Color2");

    // 2nd, creates a CSV parser with the configs
    CsvParser parser = new CsvParser(settings);

    // 3rd, parses all rows of data in selected columns from the CSV file into a matrix
    List<String[]> resolvedData = parser.parseAll(new FileReader("/examples/example.csv"));

    // 3rd, process the matrix with business logic
    for (String[] row : resolvedData) {
        StringBuilder strBuilder = new StringBuilder();
        for (String col : row) {
            strBuilder.append(col).append("\t");
        }
        System.out.println(strBuilder);
    }
}

And you will get output data for these 3 columns:

red blue gray
blue yellow white

xiaolei yu
  • 121
  • 3
1

CsvReader has a get() method to read a column by name. The CsvWriter counterpart cannot write to a named column though, so it might only solve half your problem.

andyb
  • 43,435
  • 12
  • 121
  • 150