【问题标题】:No header mapping was specified, the record values can't be accessed by name (Apache Commons CSV)未指定标头映射,无法按名称访问记录值(Apache Commons CSV)
【发布时间】:2015-02-04 01:56:07
【问题描述】:

我在尝试读取 csv 时收到此错误消息:

Exception in thread "main" java.lang.IllegalStateException: No header mapping was specified, the record values can't be accessed by name
at org.apache.commons.csv.CSVRecord.get(CSVRecord.java:99)
at mockdata.MockData.main(MockData.java:33)

Java 结果:1

我正在使用 Apache Commons CSV 库 1.1。尝试用谷歌搜索错误消息,我得到的唯一结果是 grepcode 等网站上的代码列表。

这是我的代码:

package mockdata;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;

public class MockData
{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException, IOException
    {
        Reader in = new InputStreamReader(MockData.class.getClassLoader()
                                .getResourceAsStream("MOCK_DATA.csv"), "UTF-8");
        Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(in);
        for (CSVRecord record : records) 
        {
            String lastName = record.get("last_name");
            String firstName = record.get("first_name");

            System.out.println("firstName: " + firstName + " lastName: " + lastName);
        }
    }

}

CSV 的内容:

first_name,last_name,address1,city,state,zip,country,phone,email
Robin,Lee,668 Kinsman Road,Hagerstown,TX,94913,United States,5-(078)623-0713,rlee0@e-recht24.de
Bobby,Moreno,68 Dorton Avenue,Reno,AZ,79934,United States,5-(080)410-6743,bmoreno1@ihg.com
Eugene,Alexander,3 Bunker Hill Court,Newark,MS,30066,United States,5-(822)147-6867,ealexander2@gmpg.org
Katherine,Crawford,3557 Caliangt Avenue,New Orleans,OR,23289,United States,2-(686)178-7222,kcrawford3@symantec.com

它位于我的 src 文件夹中。

【问题讨论】:

    标签: java csv apache-commons apache-commons-csv


    【解决方案1】:

    将 withHeader() 调用为默认的 Excel CSV 格式对我有用:

    CSVFormat.EXCEL.withHeader().parse(in);
    

    文档中的示例不是很清楚,但是可以找到here安全地引用列: 如果您的源代码包含标题记录,您可以通过使用不带参数的 withHeader(String...) 来简化代码并安全地引用列: CSVFormat.EXCEL.withHeader();

    【讨论】:

    【解决方案2】:

    这对我有用

    try (Reader in = new FileReader(f);
                    CSVParser parser = new CSVParser(in,
                            CSVFormat.EXCEL.withDelimiter(';').withHeader("Assembly Item Number", "Material Type",                              "Item Name", "Drawing Num", "Document Type", "Revision", "Status", "Drawing name",
                                    "BOM Material Type", "Component Name"));) {
                for (CSVRecord record : parser) {
                        System.out.println(record.get("Item Name"));
                }
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
    

    【讨论】:

      【解决方案3】:

      我设法使用以下代码忽略了标题名称中的空格(介于两者之间) - 通过使用 get(index) 而不是 get("header_name")。此外,当检测到空白值/行时停止读取 csv:

       CSVParser csvParser = CSVFormat.EXCEL.withFirstRecordAsHeader().parse(br);
               for (CSVRecord record : csvParser) {
                   String number= record.get(0);
                   String date = record.get("date");
                   String location = record.get("Location");
                   String lsFile = record.get(3);
                   String docName = record.get(4);
                
                   
                   if(StringUtils.isEmpty(lsFile)) {
                       break;
                   }
            }
      

      【讨论】:

        【解决方案4】:

        您需要查看URL 中每种格式的定义 定义文件的格式 试试这个:

              File f = new File("your path file ");
              Reader  readfile = new FileReader(f);
        
            Iterable<CSVRecord> records = CSVFormat.DEFAULT.withDelimiter(';').withHeader().parse(readfile);
        

        【讨论】:

          猜你喜欢
          • 2019-05-07
          • 1970-01-01
          • 1970-01-01
          • 2016-04-04
          • 1970-01-01
          • 2019-02-17
          • 2015-10-20
          • 1970-01-01
          相关资源
          最近更新 更多