These are chat archives for nrinaudo/kantan.csv

16th
Jan 2017
yesterday192
@yesterday192
Jan 16 2017 11:23
@nrinaudo I try to read csv file to case class (it is have 21 fields). If csv file has 20 or 21 columns, it read successful. Other case raise OutOfBounds errors. I want to csv reading successful if csv has exactly 21 columns. How can I do it?
By debugging, I see this code:
trait GeneratedRowDecoders {
  private def decode[A](ss: Seq[String], i: Int, da: CellDecoder[A]): DecodeResult[A] =
    if(ss.isDefinedAt(i))   da.decode(ss(i))
    // Special case, see https://github.com/nrinaudo/kantan.csv/issues/53
    else if(i == ss.length) da.decode("") // this code is cause of successful when csv has 20 columns 
    else                    DecodeResult.outOfBounds(i)
Nicolas Rinaudo
@nrinaudo
Jan 16 2017 12:05
@yesterday192 I don't think I've understood your problem. Can you share the corresponding code, and maybe sample data?
oh, right. You want parsing to fail if the CSV has 20 columns but succeed if it has 21, right?
I think that's how it should behave, unless your last field is of type Option[A]. Can I see your case class, decoder and data sample?
Evgeny Kurnevsky
@kurnevsky
Jan 16 2017 12:26
@nrinaudo: hi, can I somehow write RowDecoder using column names instead of indices? Or maybe there is some convenient way to get indices by names?
Nicolas Rinaudo
@nrinaudo
Jan 16 2017 12:27
@kurnevsky that's not built-in kantan.csv, I'm afraid
when you say name, you mean from the header, right?
Evgeny Kurnevsky
@kurnevsky
Jan 16 2017 12:28
nrinaudo: yes.
Nicolas Rinaudo
@nrinaudo
Jan 16 2017 12:28
yeah, that's not something that kantan.csv supports yet.
you can open an issue and if I can think of a clever way to add support to it, I'll implement it
Evgeny Kurnevsky
@kurnevsky
Jan 16 2017 12:29
nrinaudo: ok, thanks :)
Nicolas Rinaudo
@nrinaudo
Jan 16 2017 12:29
but if your need is urgent, I'm afraid this is not something I'll have a useful answer for within a reasonable timeframe
Evgeny Kurnevsky
@kurnevsky
Jan 16 2017 12:32
nrinaudo: at the moment I use scala-csv library and read csv lines as Map[String, String].. But your library is more interesting :)
Nicolas Rinaudo
@nrinaudo
Jan 16 2017 12:33
(also quite a bit faster, if I say so myself: http://nrinaudo.github.io/kantan.csv/tut/benchmarks.html)
I've not even tried to implement support for named fields yet because I've never found myself dealing with CSV data where the order of columns would change, but the labels would stay coherent
is that your scenario?
(also, I want to be able to deal with CSV data that doesn't actually have a header)
Evgeny Kurnevsky
@kurnevsky
Jan 16 2017 12:41
@nrinaudo: I read csv with header from api call and columns order is not specified. Actually it's fixed but I wouldn't like to use indices...
Nicolas Rinaudo
@nrinaudo
Jan 16 2017 12:44
I understand - I'd also argue that relying on indexes is no more arbitrary than on labels, especially in a format that does not mandate a header, but that doesn't really help you
I suppose you could read the first line as a List[String] then map?
so, say that you get a CsvReader[List[String]] instance. You could read the first line and turn it into a Map[String, Int]
then have a RowDecoder[A], created by taking indexes, but then using the content of your map to convert from indexes to labels and back
then .map(row => myDecoder.decode(row)) ?
not the cleanest thing in the world, and might need some tweaking, but it ought to work
Evgeny Kurnevsky
@kurnevsky
Jan 16 2017 12:49
nrinaudo: yeah, thanks, I also thought about this solution :)
Nicolas Rinaudo
@nrinaudo
Jan 16 2017 15:12
@kurnevsky if you manage to get something working, would you mind sharing it? It might serve as the basis for proper support in kantan.csv
Evgeny Kurnevsky
@kurnevsky
Jan 16 2017 15:15
@nrinaudo: I haven't tried yet...
Nicolas Rinaudo
@nrinaudo
Jan 16 2017 15:16
if you do, I'd love to see the result :)
Evgeny Kurnevsky
@kurnevsky
Jan 16 2017 15:16
ok :)