Read CSV file column by column

前端 未结 8 1686
萌比男神i
萌比男神i 2020-11-29 04:31

I want to read specific columns from a multi column csv file and print those columns in other csv file using Java. Any help please? Following is my code to print each token

相关标签:
8条回答
  • 2020-11-29 05:20

    Reading a CSV file in very simple and common in Java. You actually don't require to load any extra third party library to do this for you. CSV (comma separated value) file is just a normal plain-text file, store data in column by column, and split it by a separator (e.g comma ",").

    In order to read specific columns from the CSV file, there are several ways. Simplest of all is as below:

    Code to read CSV without any 3rd party library

    BufferedReader br = new BufferedReader(new FileReader(csvFile));
    while ((line = br.readLine()) != null) {
        // use comma as separator
        String[] cols = line.split(cvsSplitBy);
        System.out.println("Coulmn 4= " + cols[4] + " , Column 5=" + cols[5]);
    }
    

    If you notice, nothing special is performed here. It is just reading a text file, and spitting it by a separator – ",".

    Consider an extract from legacy country CSV data at GeoLite Free Downloadable Databases

    "1.0.0.0","1.0.0.255","16777216","16777471","AU","Australia"
    "1.0.1.0","1.0.3.255","16777472","16778239","CN","China"
    "1.0.4.0","1.0.7.255","16778240","16779263","AU","Australia"
    "1.0.8.0","1.0.15.255","16779264","16781311","CN","China"
    "1.0.16.0","1.0.31.255","16781312","16785407","JP","Japan"
    "1.0.32.0","1.0.63.255","16785408","16793599","CN","China"
    "1.0.64.0","1.0.127.255","16793600","16809983","JP","Japan"
    "1.0.128.0","1.0.255.255","16809984","16842751","TH","Thailand"
    

    Above code will output as below:

    Column 4= "AU" , Column 5="Australia"
    Column 4= "CN" , Column 5="China"
    Column 4= "AU" , Column 5="Australia"
    Column 4= "CN" , Column 5="China"
    Column 4= "JP" , Column 5="Japan"
    Column 4= "CN" , Column 5="China"
    Column 4= "JP" , Column 5="Japan"
    Column 4= "TH" , Column 5="Thailand"
    

    You can, in fact, put the columns in a Map and then get the values simply by using the key.

    Shishir

    0 讨论(0)
  • I am sorry, but none of these answers provide an optimal solution. If you use a library such as OpenCSV you will have to write a lot of code to handle special cases to extract information from specific columns.

    For example, if you have rows with less columns than what you're after, you'll have to write a lot of code to handle it. Using the OpenCSV example:

      CSVReader reader = new CSVReader(new FileReader(strFile));
      String [] nextLine;
      while ((nextLine = reader.readNext()) != null) {
           //let's say you are interested in getting columns 20, 30, and 40
           String[] outputRow = new String[3];
           if(parsedRow.length < 40){
                outputRow[2] = null;
           } else {
                outputRow[2] = parsedRow[40]
           }
           if(parsedRow.length < 30){
                outputRow[1] = null;
           } else {
                outputRow[1] = parsedRow[30]
           }
           if(parsedRow.length < 20){
                outputRow[0] = null;
           } else {
                outputRow[0] = parsedRow[20]
           }
    
      }
    

    This is a lot of code for a simple requirement. It gets worse if you are trying to get values of columns by name. You should use a more modern parser such as the one provided by uniVocity-parsers.

    To reliably and easily get the columns you want, simply write:

    CsvParserSettings settings = new CsvParserSettings();
    parserSettings.selectIndexes(20, 30, 40);
    CsvParser parser = new CsvParser(settings);
    List<String[]> allRows = parser.parseAll(new FileReader(yourFile));
    

    Disclosure: I am the author of this library. It's open-source and free (Apache V2.0 license).

    0 讨论(0)
提交回复
热议问题