1

Is there a Java library that would allow me to parse CSV files that have headers defined on multiple lines? Here's an example for such a CSV :

$ID$,$Customer$

Cust1, Jack

Cust2 , Rose

$Name$,$Location$

Sherlock,London

Clouseau,Paris

The "$" symbol indicates the presence of headers on that line, and the values in subsequent rows map to these headers.

1
  • 2
    Most CSV parsers will just parse the lines. It's up to you to interpret the first line as a header. Or not. So it's up to you to recognize a second header too. Commented Sep 22, 2015 at 17:09

2 Answers 2

1

Not sure about your particular use case, but you can simply read all rows and check when a header row appears.

univocity-parsers allows you to handle this without any trouble.

CsvParserSettings settings = new CsvParserSettings();
CsvParser parser = new CsvParser(settings);
List<String[]> allRows = parser.parseAll(new FileReader(new File("/path/to/your.csv")));
for(String[] row : allRows){
     if(isHeaderRow(row)){ // isHeaderRow() would test whether each element is enclosed within $
          // do stuff to "switch" to a new header row.
     } else {
          // do stuff
     }
}

Disclosure: I am the author of this library. It's open-source and free (Apache V2.0 license).

Sign up to request clarification or add additional context in comments.

1 Comment

Got the 1Gb csv file both with unescaped quotes and multi-line values. Much thanks to @jeronimo-backes for his univocity-parsers. Small memory footprint, flexible settings, friendly support.
0

You can use JEzCSV for Parsing the fichier csv with anytype dimiliter that you want

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.