How to parse a CSV file that can have one of two delimiters?

In my case, valid CSVs are those that are limited to a comma or semicolon. I am open to other libraries, but it should be Java. Reading through the Apache CSVParser API, the only thing I can think of is to do this, which seems inefficient and ugly.

try { BufferedReader reader = new BufferedReader(new InputStreamReader(file)); CSVFormat csvFormat = CSVFormat.EXCEL.withHeader().withDelimiter(';'); CSVParser parser = csvFormat.parse( reader ); // now read the records } catch (IOException eee) { try { // try the other valid delimeter csvFormat = CSVFormat.EXCEL.withHeader().withDelimiter(','); parser = csvFormat.parse( reader ); // now read the records } catch (IOException eee) { // then its really not a valid CSV file } } 

Is there a way to check the delimiter first, or perhaps allow two delimiters? Does anyone have an idea better than just catching an exception?

+7
java csv apache-commons-csv
source share
1 answer

We built support for this in uniVocity-parsers :

 public static void main(String... args) { CsvParserSettings settings = new CsvParserSettings(); settings.setDelimiterDetectionEnabled(true); CsvParser parser = new CsvParser(settings); List<String[]> rows = parser.parseAll(file); } 

There are many other functions in the parser that I am sure you will find useful. Give it a try.

Disclaimer: I am the author of this library, open source and free (apache 2.0 license)

+2
source share

All Articles