6

Is there a built-in feature in Apache CSV implementation to ignore header case when reading csv?

Some time ago we have implemented utility for converting CSV export files to SQL. For that we have choosen Apache CSV. Everything was perfectly fine until now, but yet now we've got a change request.

All CSV files we process must contain headers and now we should read those headers in case insensitive way, so our users don't have to put their effort in it and watch if CSV they created follows our header case requirments.

Code sample:

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

where Data.NAME is

public static final String NAME = "Name";

And the problem obviously raises when the user uses "name" as column header in his CSV instead of "Name" with capital "N".

I have followed both the API and source but couldn't find anything. Is there a way how to force the CSVRecord use CaseInsensitiveMap for its mapping or anything similiar?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Kousalik
  • 3,111
  • 3
  • 24
  • 46

3 Answers3

6

This feature was added to Apache Commons CSV 1.3 as CSV-159

Committed revision 1708685.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Gary Gregory
  • 439
  • 5
  • 7
  • 2
    To use this feature: CSVParser parser = CSVFormat.EXCEL.withHeader().withIgnoreHeaderCase().parse(reader); – Kt Mack Jun 29 '20 at 21:05
1

I think you have to handle csv headers to your convenience, before you parse CSV file with apache-commons-csv, you can do this with RandomAccessFile to overwrite case in first line (headers)

//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: I think is very difficult to achieve this in some other way, since is a closed api (every class is final and the headers mapping returns a copy of the internal headers map)

yamilmedina
  • 3,315
  • 2
  • 20
  • 28
-1

I haven't used Apache CSV but does the below help

Map<String, String> nodeMap = 
    new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
Sam Thadhani
  • 585
  • 1
  • 10
  • 21
  • That is indeed nice but doesn't help me in any way. I could come with dozens of case insensitive solutions but at first I'd like to be sure there is no support for that in Apache CSV before I go for another library and start rewriting all parsing. I'm sure there are libraries out there which supports that and its easy but now my question is on Apache CSV. – Kousalik Apr 28 '15 at 06:15
  • Why not then just extend the class with a custom class and override the get method with String in the signature in that case you won't have to rewrite anything. – Sam Thadhani Apr 28 '15 at 06:29
  • You mean download commons csv sources and patch it as I need instead of using it as dependency ? Can't see any other way how extending class (I suppose you meant CSVRecord which is part of Apaches code and used internaly) could be helpful. Or I don't really understand your idea. Could you explain? – Kousalik Apr 28 '15 at 11:12
  • I meant use it as a dependency and just extend it by extends keyword and annotate the get method or any method with override and then use the new class as map and when you will do get it will call your class. OOPS is the way to go. – Sam Thadhani Apr 29 '15 at 08:37
  • Yes, that's what I thought. But one can't just use new class inside existing library. The original class is used inside the library, hardcoded in its classes. See ? – Kousalik Apr 29 '15 at 09:16