强类型访问csv在scala? [英] Strongly typed access to csv in scala?

查看:89
本文介绍了强类型访问csv在scala?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以强类型方式访问scala中的csv文件。例如,当我读取csv的每一行时,它会被自动解析并表示为具有适当类型的元组。我可以在传递给解析器的某种模式中预先指定类型。有没有任何图书馆存在这样做?如果没有,我该如何自行实现这个功能?

I would like to access csv files in scala in a strongly typed manner. For example, as I read each line of the csv, it is automatically parsed and represented as a tuple with the appropriate types. I could specify the types beforehand in some sort of schema that is passed to the parser. Are there any libraries that exist for doing this? If not, how could I go about implementing this functionality on my own?

推荐答案

product-collections 似乎非常适合您的需求:

product-collections appears to be a good fit for your requirements:

scala> val data = CsvParser[String,Int,Double].parseFile("sample.csv")
data: com.github.marklister.collections.immutable.CollSeq3[String,Int,Double] = 
CollSeq((Jan,10,22.33),
        (Feb,20,44.2),
        (Mar,25,55.1))

产品集合使用 opencsv

product-collections uses opencsv under the hood.

A CollSeq3 IndexedSeq [Product3 [T1,T2,T3]] 以及 Product3 [Seq [T1],Seq [T2],Seq [T3]] 。我是产品收藏的作者。

A CollSeq3 is an IndexedSeq[Product3[T1,T2,T3]] and also a Product3[Seq[T1],Seq[T2],Seq[T3]] with a little sugar. I am the author of product-collections.

以下是指向scaladoc的io页面的链接

Product3本质上是一个arity 3的元组。

Product3 is essentially a tuple of arity 3.

这篇关于强类型访问csv在scala?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆