You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Problem ezload provides a way to read CSV checking its format. But it is too focused on writing to a database when it is also useful to just read CSV. Actually, to read CSV you have to do this
while ((raw = bufferedReader.readLine()) != null) {
LineparsedLine = this.parser.parse(raw);
for (Valuevalue : parsedLine.values()) {
value.accept(newSomeAction(...));
}
}
In this example, users have to implement an action to take values from csv for each line.
The solution I'd like
provide a ReadCsv to read easily CSV lines by columns. Maybe something like this
The problem of the example above is that a line has multiple columns with different types and we don't want to cast. So, considering that every time we read a line the parser checks its format. When we retrieve a value, we can assume that the line is correct. Otherwise, an exception should have been thrown and in that case, we didn't even have the chance to read a column from a wrong line. Also, we already know the types that ezload allows to read. So we can do this
When we call dateValue and that column is an int we can throw an exception or try to parse the value into a LocalTime.
All these tries are not elegant enough. Another option is to have a Map<String, ?> for each type. Then the user can get them just calling them by its name
inti = line.intFrom("column name");
Strings = line.stringFrom("another column name");
doubled = line.doubleFrom("you got the idea right?");
Now if the user asks for an int that has been defined as String this throws an exception because that column name does not exist on int's map. Then the full example is
ReadCsvread = newReadImplementation(Parser, Path, StandarCharset);
while(read.hasNext()) {
MapLineline = read.next();
inti = line.intFrom("column name");
Strings = line.stringFrom("another column name");
doubled = line.doubleFrom("you got the idea right?");
}
The text was updated successfully, but these errors were encountered:
Problem
ezload
provides a way to read CSV checking its format. But it is too focused on writing to a database when it is also useful to just read CSV. Actually, to read CSV you have to do thisIn this example, users have to implement an action to take values from csv for each line.
The solution I'd like
provide a
ReadCsv
to read easily CSV lines by columns. Maybe something like thisThe problem of the example above is that a line has multiple columns with different types and we don't want to cast. So, considering that every time we read a line the parser checks its format. When we retrieve a value, we can assume that the line is correct. Otherwise, an exception should have been thrown and in that case, we didn't even have the chance to read a column from a wrong line. Also, we already know the types that
ezload
allows to read. So we can do thisWhen we call
dateValue
and that column is anint
we can throw an exception or try to parse the value into aLocalTime
.All these tries are not elegant enough. Another option is to have a
Map<String, ?>
for each type. Then the user can get them just calling them by its nameNow if the user asks for an
int
that has been defined asString
this throws an exception because that column name does not exist on int's map. Then the full example isThe text was updated successfully, but these errors were encountered: