【问题标题】:Apache Commons CSV Mapping not found未找到 Apache Commons CSV 映射
【发布时间】:2019-05-07 15:58:02
【问题描述】:

我正在尝试使用 Apache Commons CSV 将具有某些标头的 CSV 文件读入 Java 对象。但是,当我运行代码时,我得到以下异常:

Exception in thread "main" java.lang.IllegalArgumentException: Mapping for Color not found, expected one of [Color, Name, Price, House Cost, Rent, 1 House, 2 Houses, 3 Houses, 4 Houses, Hotel, Mortgage]
at org.apache.commons.csv.CSVRecord.get(CSVRecord.java:102)
at GameBoard.<init>(GameBoard.java:25)
at Game.main(Game.java:3)

有人可以解释异常的来源吗?在我看来,Apache Commons 不知何故与我的输入不匹配。我有什么问题还是有其他问题?这是我的代码 sn-p:

Reader in;
    Iterable<CSVRecord> records = null;
    try {
        in = new FileReader(new File(Objects.requireNonNull(getClass().getClassLoader().getResource("Properties.csv")).getFile()));
        records = CSVFormat.EXCEL.withFirstRecordAsHeader().parse(in);
    } catch (IOException | NullPointerException e) {
        e.printStackTrace();
        System.exit(1);
    }
    for (CSVRecord record :
            records) {
        spaces.add(new Property(
                record.get("Color"),
                record.get("Name"),
                Integer.parseInt(record.get("Price")),

这是我的 csv 标头(抱歉,一个被切断了,但这不是重点):

谢谢!

【问题讨论】:

  • 我想知道它是否与这个问题有关:stackoverflow.com/questions/10277348/… 但如果是这样,我不知道如何解决它。
  • 看起来像一个奇怪的不匹配,它与列表中的标题不匹配。可能是 CSV 文件中的一些特殊字符或尾随空格,您能否将 csv 文件显示为文本,即 csv 文件的内容。

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


【解决方案1】:

我有同样的问题,只有当你引用第一列时才会发生,所有其他列名都在工作。问题是,UTF-8 表示会预先添加以下字符“0xEF,0xBB,0xBF”(请参阅​​Wikipedia page)。这似乎是 commons-csv 的一个已知问题,但由于这是特定于应用程序的,因此无法修复 (CSVFormat.EXCEL.parse should handle byte order marks)。

但是,有一个记录在案的解决方法:

http://commons.apache.org/proper/commons-csv/user-guide.html#Handling_Byte_Order_Marks

【讨论】:

  • 这应该被标记为接受。请注意,在问题中,问题也与第一列有关。
【解决方案2】:

我遇到了同样奇怪的异常。它实际上说“期待...”,然后列出它说找不到的字段 - 就像你的情况一样。

原因是我设置了错误的CSVFormat:

CSVFormat csvFormat = CSVFormat.newFormat(';');

这意味着我的代码试图在一个实际上有 逗号 分隔符的文件中分隔 分号 上的字段。

一旦我使用了 DEFAULT CSVFormat,一切都开始工作了。

CSVFormat csvFormat = CSVFormat.DEFAULT;

所以答案可能是您必须为您的文件正确设置 CSVFormat。

【讨论】:

    【解决方案3】:

    通过在 CSV 文件中提供不同的标题名称(如 xyz)或尝试通过调用 csvRecord.get("x_z") 来获取值,我也遇到了同样的异常

    我解决了更改标题名称xyz 的问题。

    try {
      fileReader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
      csvParser = new CSVParser(fileReader,
          CSVFormat.DEFAULT.withFirstRecordAsHeader().withIgnoreHeaderCase().withTrim());
      Iterable<CSVRecord> csvRecords = csvParser.getRecords();
      CSVFormat csvFormat = CSVFormat.DEFAULT;
      for (CSVRecord csvRecord : csvRecords) {
    
    
    } catch (Exception e) {
      System.out.println("Reading CSV Error!");
      e.printStackTrace();
    } finally {
      try {
        fileReader.close();
        csvParser.close();
      } catch (IOException e) {
        System.out.println("Closing fileReader/csvParser Error!");
        e.printStackTrace();
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-02-04
      • 2016-07-23
      • 1970-01-01
      • 1970-01-01
      • 2017-02-24
      • 2017-05-30
      • 1970-01-01
      • 1970-01-01
      • 2017-12-11
      相关资源
      最近更新 更多