Skip to content

Commit

Permalink
read first 5 lines of csv to check delimiter
Browse files Browse the repository at this point in the history
  • Loading branch information
simei94 committed May 15, 2024
1 parent f623322 commit 3dd1f0d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public final class CsvOptions {
@CommandLine.Option(names = "--csv-format", description = "CSV Format", defaultValue = "Default")
private CSVFormat.Predefined csvFormat;

@CommandLine.Option(names = "--csv-delimiter", description = "CSV Delimiter", required = false)
@CommandLine.Option(names = "--csv-delimiter", description = "CSV Delimiter")
private Character csvDelimiter;

@CommandLine.Option(names = "--csv-charset", description = "CSV input encoding", defaultValue = "UTF8")
Expand Down Expand Up @@ -56,24 +56,48 @@ public CsvOptions(CSVFormat.Predefined csvFormat, Character csvDelimiter, Charse
*/
public static Character detectDelimiter(String path) throws IOException {
try (BufferedReader reader = IOUtils.getBufferedReader(path)) {
String firstLine = reader.readLine();
int[] comma = new int[5];
int[] semicolon = new int[5];
int[] tab = new int[5];
String[] lines = new String[5];

// check five first lines for separator chars. It might be that the csv file has additional info in the first x lines (e.g. EPSG)
for (int i = 0; i < 5; i++) {
lines[i] = reader.readLine();
if (lines[i] == null) {
comma[i] = 0;
semicolon[i] = 0;
tab[i] = 0;
} else {
comma[i] = StringUtils.countMatches(lines[i], ",");
semicolon[i] = StringUtils.countMatches(lines[i], ";");
tab[i] = StringUtils.countMatches(lines[i], "\t");
}
}

int comma = StringUtils.countMatches(firstLine, ",");
int semicolon = StringUtils.countMatches(firstLine, ";");
int tab = StringUtils.countMatches(firstLine, "\t");
Integer index = null;

if (comma == 0 && semicolon == 0 && tab == 0) {
throw new IllegalArgumentException("No delimiter found in the first line of the file.");
for (int i = 0; i < comma.length - 1; i++) {
// only check next index if line with separators was not found
if (index == null) {
if (!(comma[i] == 0 && semicolon[i] == 0 && tab[i] == 0)) {
index = i;
}
}
}

// Comma is preferred as the more likely format
if (comma >= semicolon && comma >= tab) {
return ',';
} else if (tab >= semicolon)
return '\t';
else
return ';';
}
if (index == null) {
throw new IllegalArgumentException("No delimiter found in the first line of the file.");
} else {
// Comma is preferred as the more likely format
if (comma[index] >= semicolon[index] && comma[index] >= tab[index]) {
return ',';
} else if (tab[index] >= semicolon[index])
return '\t';
else
return ';';
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ void output() throws IOException {

printer.printRecord("header", "column");
printer.printRecord("1", "2");
printer.printRecord("3", "4");
printer.printRecord("5", "6");
printer.close();

assertThat(tmp)
.hasContent("header" + delimiter + "column\n1" + delimiter + "2");
.hasContent("header" + delimiter + "column\n1" + delimiter + "2" + "\n3" + delimiter + "4" + "\n5" + delimiter + "6");

assertThat(delimiter).isEqualTo(CsvOptions.detectDelimiter(tmp.toString()).toString());
}
Expand Down

0 comments on commit 3dd1f0d

Please sign in to comment.