Return-Path: X-Original-To: apmail-commons-issues-archive@minotaur.apache.org Delivered-To: apmail-commons-issues-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 3E6A89852 for ; Thu, 15 Mar 2012 01:45:02 +0000 (UTC) Received: (qmail 29240 invoked by uid 500); 15 Mar 2012 01:45:01 -0000 Delivered-To: apmail-commons-issues-archive@commons.apache.org Received: (qmail 29148 invoked by uid 500); 15 Mar 2012 01:45:01 -0000 Mailing-List: contact issues-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: issues@commons.apache.org Delivered-To: mailing list issues@commons.apache.org Received: (qmail 29140 invoked by uid 99); 15 Mar 2012 01:45:01 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 15 Mar 2012 01:45:01 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED,T_RP_MATCHES_RCVD X-Spam-Check-By: apache.org Received: from [140.211.11.116] (HELO hel.zones.apache.org) (140.211.11.116) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 15 Mar 2012 01:44:59 +0000 Received: from hel.zones.apache.org (hel.zones.apache.org [140.211.11.116]) by hel.zones.apache.org (Postfix) with ESMTP id 178BB20A09 for ; Thu, 15 Mar 2012 01:44:38 +0000 (UTC) Date: Thu, 15 Mar 2012 01:44:38 +0000 (UTC) From: "Sebb (Commented) (JIRA)" To: issues@commons.apache.org Message-ID: <184376945.17062.1331775878097.JavaMail.tomcat@hel.zones.apache.org> In-Reply-To: <1815982471.16917.1331772039507.JavaMail.tomcat@hel.zones.apache.org> Subject: [jira] [Commented] (CSV-65) Header support MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 X-Virus-Checked: Checked by ClamAV on apache.org [ https://issues.apache.org/jira/browse/CSV-65?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13229825#comment-13229825 ] Sebb commented on CSV-65: ------------------------- If the file does not provide a header record, it should be possible for the user to provide one. > Header support > -------------- > > Key: CSV-65 > URL: https://issues.apache.org/jira/browse/CSV-65 > Project: Commons CSV > Issue Type: New Feature > Components: Parser > Reporter: Emmanuel Bourg > Fix For: 1.0 > > > 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: > {code:java} > CSVParser parser = new CSVParser(in); > Iterator it = parser.iterator(); > // read the header > String[] header = it.next(); > // build a name to index mapping > Map 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); > } > {code} > The header should be defined in the format with something like this: > {code:java} > CSVFormat format = CSVFormat.DEFAULT.withHeader(); > {code} > Then either the parser provides the column name to index mapping automatically: > {code:java} > 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); > } > {code} > or the parser returns a Map like structure similar to a JDBC ResultSet (replacing String[]): > {code:java} > 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); > } > {code} -- This message is automatically generated by JIRA. If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa For more information on JIRA, see: http://www.atlassian.com/software/jira