Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read first 5 lines of csv to check delimiter #3271

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading