Details

    • Type: New Feature New Feature
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.0
    • Component/s: Parser
    • Labels:
      None

      Description

      Commons CSV is missing some elements to help dealing with CSV file headers.

      With the current API one has to write the following code to read the fields by name:

      CSVParser parser = new CSVParser(in);
      Iterator<String[]> it = parser.iterator();
      
      // read the header
      String[] header = it.next();
      
      // build a name to index mapping
      Map<String, Integer> mapping = new HashMap<>();
      for (int i = 0; i < header.length; i++) {
          mapping.put(header[i], i);
      }
      
      // parse the records
      for (String[] record : parser) {
          Person person = new Person();
          person.setName(record[mapping.get("name")]);
          person.setEmail(record[mapping.get("email")]);
          person.setPhone(record[mapping.get("phone")]);
          persons.add(person);
      }
      

      The header should be defined in the format with something like this:

      CSVFormat format = CSVFormat.DEFAULT.withHeader();
      

      Then either the parser provides the column name to index mapping automatically:

      CSVFormat format = CSVFormat.DEFAULT.withHeader();
      CSVParser parser = new CSVParser(in, format);
      
      // parse the records
      for (String[] record : parser) {
          Person person = new Person();
          person.setName(record[parser.indexOf("name")]);
          person.setEmail(record[parser.indexOf("email")]);
          person.setPhone(record[parser.indexOf("phone")]);
          persons.add(person);
      } 
      

      or the parser returns a Map like structure similar to a JDBC ResultSet (replacing String[]):

      CSVFormat format = CSVFormat.DEFAULT.withHeader();
      
      for (CSVRecord record : format.parse(in)) {
          Person person = new Person();
          person.setName(record.get("name"));
          person.setEmail(record.get("email"));
          person.setPhone(record.get("phone"));
          persons.add(person);
      } 
      
      1. CSVFormat.java
        18 kB
        Emmanuel Bourg
      2. CSVParser.java
        9 kB
        Emmanuel Bourg
      3. CSVRecord.java
        2 kB
        Emmanuel Bourg

        Issue Links

          Activity

          Emmanuel Bourg created issue -
          Emmanuel Bourg made changes -
          Field Original Value New Value
          Link This issue supercedes CSV-44 [ CSV-44 ]
          Emmanuel Bourg made changes -
          Attachment CSVRecord.java [ 12518450 ]
          Attachment CSVFormat.java [ 12518451 ]
          Attachment CSVParser.java [ 12518452 ]
          Sebb made changes -
          Status Open [ 1 ] Resolved [ 5 ]
          Resolution Fixed [ 1 ]
          Benedikt Ritter made changes -
          Status Resolved [ 5 ] Closed [ 6 ]

            People

            • Assignee:
              Unassigned
              Reporter:
              Emmanuel Bourg
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development