简体   繁体   中英

Apache Commons CSV Mapping not found

I am trying to read a CSV file with certain headers into a Java object using Apache Commons CSV. However, when I run the code, I get the following exeption:

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)

Can someone explain where the exception is coming from? It appears to me that Apache Commons somehow is not matching my input to a column. Is there something wrong on my part or is something else broken? Here is my code snippet:

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")),

And here are my csv headers (sorry, one was cut off but that's not the point): csv 标头

Thanks!

I had the same probem which only occurs if you reference the first column, all other column names are working. The problem is, that the UTF-8 representation prepends the following characters "0xEF,0xBB,0xBF" (see Wikipedia page ). This seems to be a known problem for commons-csv but since this is application specific, it won't be fixed ( CSVFormat.EXCEL.parse should handle byte order marks ).

However, there is a documented workaround for this:

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

I got the same weird exception. It actually said "Expecting one of ..." and then listed the field it said it could not find - just like in your case.

The reason was that I had set the wrong CSVFormat:

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

This meant that my code was trying to separate fields on semi-colons in a file that actually had comma separators.

Once I used the DEFAULT CSVFormat, everything started to work.

CSVFormat csvFormat = CSVFormat.DEFAULT;

So the answer is that probably you must set CSVFormat correctly for your file.

Moving to spring boot version 2.6.7 from 2.4.5 brought about this error.. I had to convert each csvRecord to a map before assigning it to my POJO as follows.

for (CSVRecord csvRecord : csvRecords) {

            Map<String, String> csvMap = csvRecord.toMap();

            Model newModel = new Model();
           model.setSomething(csvMap.get("your_item"));
        }

在此处输入图像描述

I also got the same exception by giving a different name of header in CSV file like xyz , or trying to get the value by calling csvRecord.get("x_z")

I resolved my problem changing the header name 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();
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM