How to read from a specific header in opencsv?

I have a csv file. I want to extract a specific column from it. For example: Let's say I have a 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 the data from the caste1 header?

+3


source to share


3 answers


There are no built-in functions in opencsv to read from a column by name.

the official FAQ contains the following example of how to read from a file:

CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
   // nextLine[] is an array of values from the line
   System.out.println(nextLine[0] + nextLine[1] + "etc...");
}

      

You simply extract the value in the second column for each row by concatenating the row with nextLine[1]

(remember that array indices are zero-based.)



So, in your case, you can simply read from the second line:

CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
   System.out.println(nextLine[1]);
}

      


For a more sophisticated way of determining the index of a column from its header see the answer from Scott Conway .

+1


source


Magnilex and Sparky are correct that CSVReader does not support reading values โ€‹โ€‹by column name. But, having been said, there are two ways you can do this.

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

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

      

to make your method look like (leaving a lot of error checking you will need to add)

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 this if you were pressed for a time and it was only one column that you cared about. This is because openCSV can directly convert an object that has variables just like the header column names using the CsvToBean

and class HeaderColumnNameMappingStrategy

.

So, first you would define a class that has fields (and you really only need to put the fields you want), and extras are ignored, and missing ones with zero 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 will 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());
}

      

+4


source


Looking at the javadoc

if you create a CSVReader object you can use the .readAll method to pull out the whole file. It returns a list of strings [], with each string [] representing a line in the file. So now you have the markers for each line and you only need the second element, so separate them the way they were nicely provided to you with delimiters. And on each line, you only need the second element, so:

public static void main(String[] args){
    String data = "63,Graham,101153.06,Abraham";
    String result[] = data.split(",");
    System.out.print(result[1]);
}

      

-1


source







All Articles