Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read from particular header in opencsv?

Tags:

java

opencsv

I have a csv file. I want to extract particular column from it.For example: Say, I have csv:

id1,caste1,salary,name1
63,Graham,101153.06,Abraham
103,Joseph,122451.02,Charlie
63,Webster,127965.91,Violet
76,Smith,156150.62,Eric
97,Moreno,55867.74,Mia
65,Reynolds,106918.14,Richard

How can i use opencsv to read only data from header caste1?

like image 392
Yashasvi Raj Pant Avatar asked Jul 14 '15 18:07

Yashasvi Raj Pant


People also ask

How to read data from CSV file in Java using OpenCSV?

For reading data line by line, first we have to construct and initialize CSVReader object by passing the filereader object of CSV file. After that we have to call readNext() method of CSVReader object to read data line by line as shown in below code.


2 Answers

From the opencsv docs:

Starting with version 4.2, there’s another handy way of reading CSV files that doesn’t even require creating special classes. If your CSV file has headers, you can just initialize a CSVReaderHeaderAware and start reading the values out as a map:

  reader = new CSVReaderHeaderAware(new FileReader("yourfile.csv"));
  record = reader.readMap();

.readMap() will return a single record. You need to call .readMap() repeatedly to get all the records until you get null when it runs to the end (or to the first empty line), e.g.:

Map<String, String> values;

while ((values = reader.readMap()) != null) {

    // consume the values here

}

The class also has another constructor which allows more customization, e.g.:

CSVReaderHeaderAware reader = new CSVReaderHeaderAware(
        new InputStreamReader(inputStream),
        0,      // skipLines
        parser, // custom parser
        false,  // keep end of lines
        true,   // verify reader
        0,      // multiline limit
        null    // null for default locale
);

One downside which I have found is that since the reader is lazy it does not offer a record count, therefore, if you need to know the total number (for example to display correct progress information), then you'll need to use another reader just for counting lines.

You also have available the CSVReaderHeaderAwareBuilder

like image 133
ccpizza Avatar answered Oct 21 '22 07:10

ccpizza


Magnilex and Sparky are right in that CSVReader does not support reading values by column name. But that being said there are two ways you can do this.

Given that you have the column names and the default CSVReader reads the header you can search the first the header for the position then use that from there on out;

private int getHeaderLocation(String[] headers, String columnName) {
   return Arrays.asList(headers).indexOf(columnName);
}

so your method would look like (leaving out a lot of error checks you will need to put in)

CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
String [] nextLine;
int columnPosition;

nextLine = reader.readNext();
columnPosition = getHeaderLocation(nextLine, "castle1");

while ((nextLine = reader.readNext()) != null && columnPosition > -1) {
   // nextLine[] is an array of values from the line
   System.out.println(nextLine[columnPosition]);
}

I would only do the above if you were pressed for time and it was only one column you cared about. That is because openCSV can convert directly to an object that has the variables the same as the header column names using the CsvToBean class and the HeaderColumnNameMappingStrategy.

So first you would define a class that has the fields (and really you only need to put in the fields you want - extras are ignored and missing ones are null or default values).

public class CastleDTO {
   private int id1;
   private String castle1;
   private double salary;
   private String name1;

   // have all the getters and setters here....
}

Then your code would look like

CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
HeaderColumnNameMappingStrategy<CastleDTO> castleStrategy = new HeaderColumnNameMappingStrategy<CastleDTO>();
CsvToBean<CastleDTO> csvToBean = new CsvToBean<CastleDTO>();

List<CastleDTO> castleList = csvToBean.parse(castleStrategy, reader);

for (CastleDTO dto : castleList) {
   System.out.println(dto.getCastle1());
}
like image 45
Scott Conway Avatar answered Oct 21 '22 06:10

Scott Conway