user3231104
user3231104

Reputation: 83

Parser for CSV with headers on multiple lines using Java

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.

Upvotes: 1

Views: 2195

Answers (2)

Yiao SUN
Yiao SUN

Reputation: 978

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

Upvotes: 0

Jeronimo Backes
Jeronimo Backes

Reputation: 6289

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).

Upvotes: 1

Related Questions