Apache Commons CSV - case insensitive headers?

Is there a built-in function in Apache CSV to ignore the header when reading csv?

Some time ago, we introduced a utility for converting CSV export files to SQL. For this, we chose Apache CSV. Everything has been fine so far, but now we have a change request.

All the CSV files that we process must contain headers, and now we must read these headers case insensitive, so our users do not need to make any effort to ensure that the CSVs they created comply with our header requirements.

Code example:

for (CSVRecord record : subsidiaryRows) {
    String name = record.get(Data.NAME));

where Data.NAME

public static final String NAME = "Name";

And the problem clearly arises when the user uses the “name” as the column heading in his CSV instead of “Name” with a capital of “N”.

I followed the API and source, but did not find anything. Is there a way to get CSVRecord to use CaseInsensitiveMap to match it or something like that?

+4
source share
3 answers

This feature was added in Apache Commons CSV 1.3 as CSV-159.

Revised version 1708685.

+5
source

, csv , CSV apache-commons-csv, RandomAccessFile ()

//convert headers to my 'known' format (ie: lowercase)
RandomAccessFile raf = new RandomAccessFile("C:\\test.csv", "rw");
String firstLine = raf.readLine();
raf.seek(0);
raf.writeBytes(firstLine.toLowerCase());

//your normal process to parse CSV with commons csv

PS: , - , api ( , )

0

I have not used Apache CSV, but does the following help

Map<String, String> nodeMap = 
    new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
-1
source

Source: https://habr.com/ru/post/1585030/


All Articles