• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Scala CSVReader类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Scala中com.github.tototoshi.csv.CSVReader的典型用法代码示例。如果您正苦于以下问题:Scala CSVReader类的具体用法?Scala CSVReader怎么用?Scala CSVReader使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了CSVReader类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Scala代码示例。

示例1: ResultWriter

//设置package包名称以及导入依赖的类
package org.hpi.esb.commons.output.writers

import java.io.File

import com.github.tototoshi.csv.CSVReader
import org.hpi.esb.commons.output.CSVOutput
import org.hpi.esb.commons.output.model.Result
import org.hpi.esb.commons.util.Logging

import scala.io.Source

abstract class ResultWriter(inputFilesPrefix: String, resultsPath: String,
                            outputFileName: String) extends Logging {

  def execute(): Unit = {
    val inputSources = getInputSources(resultsPath)
    val table = createResultTable(inputSources)
    writeResults(table, resultsPath, outputFileName)
  }

  def createResultTable(inputSources: List[Source]): List[List[String]] = {
    val intermediateResultMaps = getIntermediateResultMaps(inputSources)
    if (intermediateResultMaps.nonEmpty) {
      val finalResult = getFinalResult(intermediateResultMaps)
      finalResult.toTable()
    } else {
      logger.info("No benchmark series result files were found for merger.")
      sys.exit(1)
    }
  }

  def writeResults(mergedResults: List[List[String]], resultPath: String, fileName: String): Unit = {
    CSVOutput.write(mergedResults, resultPath, fileName)
  }

  def getIntermediateResultMaps(sources: List[Source]): List[Map[String, String]] = {
    val readers = sources.map(CSVReader.open)
    readers.flatMap(r => r.allWithHeaders())
  }

  def getInputSources(resultsPath: String): List[Source] = {
    val files = getListOfFiles(resultsPath)
    val filteredFiles = filterFilesByPrefix(files, inputFilesPrefix)
    filteredFiles.map(Source.fromFile)
  }

  def filterFilesByPrefix(files: List[File], prefix: String): List[File] = {
    files.filter(_.getName.startsWith(prefix))
  }

  def getListOfFiles(dir: String): List[File] = {
    val d = new File(dir)
    if (d.exists && d.isDirectory) {
      d.listFiles.filter(_.isFile).toList
    } else {
      List[File]()
    }
  }

  def getFinalResult(runResultMaps: List[Map[String, String]]): Result
} 
开发者ID:BenReissaus,项目名称:EnterpriseStreamingBenchmark,代码行数:62,代码来源:ResultWriter.scala


示例2: Data

//设置package包名称以及导入依赖的类
package task.airport
package data

import java.net.URL

import akka.NotUsed
import akka.stream.scaladsl.Source
import com.github.tototoshi.csv.CSVReader
import task.airport.model.{Airport, Country, Runway}

import scala.io.{Source => IOSource}
import scala.util.{Failure, Success, Try}

object Data {

  private val log = Logging logger getClass

  lazy val countries: Source[Country, NotUsed] =
    source(resources.countriesCSV)(Parser.country)

  lazy val airports: Source[Airport, NotUsed] =
    source(resources.airportsCSV)(Parser.airport)

  lazy val runways: Source[Runway, NotUsed] =
    source(resources.runwaysCSV)(Parser.runway)

  private def source[T](url: URL)(parse: Seq[String] => T): Source[T, NotUsed] =
    Source
      .fromIterator { () =>
        val source = IOSource fromURL url
        val reader = CSVReader open source
        val iterator = reader.iterator
        iterator
      }
      .drop(1)
      .map { row =>
        Try {
          parse(row)
        } match {
          case Success(value) =>
            value
          case Failure(exception) =>
            // It might be dangerous to log the CSV row.
            log.warn(s"Failed to parse: $row")
            throw exception
        }
      }

} 
开发者ID:rarrabi,项目名称:task-airport,代码行数:50,代码来源:Data.scala


示例3: BatchService

//设置package包名称以及导入依赖的类
package sug.batchservice

import java.io.File

import com.github.tototoshi.csv.CSVReader
import com.typesafe.scalalogging.StrictLogging
import org.apache.commons.io.monitor.{FileAlterationListenerAdaptor, FileAlterationMonitor, FileAlterationObserver}

object BatchService extends App with StrictLogging {

  def processor(person: Person): Person = person match {
    case Person(fn, ln) => Person(fn.toUpperCase,ln.toUpperCase)
  }

  case class Person(firstName: String, lastName: String) {
    override def toString: String = s"firstName: $firstName lastName: $lastName"
  }

  def monitor(directory: String, interval: Long = 1000) = {
    val m = new FileAlterationMonitor(interval)
    val o = new FileAlterationObserver(directory)
    m.addObserver(o)
    m.start()

    val l = new FileAlterationListenerAdaptor {
      override def onFileChange(file: File): Unit = {
        logger.info(s"File changed ${file.getName}.")
        //TODO
      }
    }
    o.addListener(l)
  }

  def reader(absolutePath: String): List[List[String]] = {
    val reader = CSVReader.open(new File(absolutePath))
    val rows = reader.all()
    logger.info(s"Read ${rows.size} row.")
    rows.map(_.map(_.trim))
  }

  def converter(raw: List[String]): Either[List[String], Person] = raw match {
    case firstName :: lastName :: Nil if lastName.nonEmpty && firstName.nonEmpty =>
      Right(Person(firstName, lastName))
    case row => Left(row)
  }

  def errorWriter(err: List[String]): Unit =
    logger.info(s"Err: $err")


  def okWriter(person: Person): Unit =
    logger.info(s"Writer: $person")
} 
开发者ID:mateuszjancy,项目名称:IntroToRxScala,代码行数:54,代码来源:BatchService.scala


示例4: LocalDataRepo

//设置package包名称以及导入依赖的类
package util

import java.io.{BufferedWriter, File, FileOutputStream, FileWriter}
import java.nio.file.{Files, Paths}

import com.github.tototoshi.csv.{CSVReader, CSVWriter, DefaultCSVFormat}
import com.google.inject.Singleton

import scala.io.Source

@Singleton
class LocalDataRepo extends BaseDataRepo {

  class CSVFormat(_delimiter: Char) extends DefaultCSVFormat {
    override val delimiter: Char = _delimiter
  }

  override def read(filePath: String): Array[Byte] = {
    Files.readAllBytes(Paths.get(filePath))
  }

  override def readCsv(filePath: String, delimiter: Char): Seq[Seq[String]] = {
    CSVReader.open(new File(filePath))(new CSVFormat(delimiter)).all()
  }

  override def readCsvAsMap(filePath: String, delimiter: Char): Seq[Map[String, String]] = {
    CSVReader.open(new File(filePath))(new CSVFormat(delimiter)).allWithHeaders()
  }

  override def readLines(filePath: String): Seq[String] = {
    Source.fromFile(filePath).getLines().toList
  }

  override def write(data: Array[Byte], filePath: String): Unit = {
    new FileOutputStream(new File(filePath)).write(data)
  }

  override def writeCsv(data: Seq[Seq[Any]], filePath: String, delimiter: Char): Unit = {
    CSVWriter.open(new File(filePath))(new CSVFormat(delimiter)).writeAll(data)
  }

  override def writeLines(data: Seq[String], filePath: String): Unit = {
    new BufferedWriter(new FileWriter(filePath)).write(data.mkString(scala.util.Properties.lineSeparator))
  }

} 
开发者ID:davidheryanto,项目名称:play-with-guice-in-scala-crude-example,代码行数:47,代码来源:LocalDataRepo.scala


示例5: CsvLoader

//设置package包名称以及导入依赖的类
package com.miriamlaurel.aggro.csv.okcoin

import java.io.{File, FilenameFilter}
import java.time.{Instant, ZoneId}
import java.time.format.DateTimeFormatter
import java.time.temporal.TemporalAccessor
import java.util.UUID

import com.github.tototoshi.csv.CSVReader
import com.miriamlaurel.aggro.Inventory
import com.miriamlaurel.aggro.model.Fill
import com.miriamlaurel.fxcore._
import com.miriamlaurel.fxcore.asset.Currency
import com.miriamlaurel.fxcore.party.Party
import com.miriamlaurel.fxcore.portfolio.Position

object CsvLoader {

  val oft = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.of("Asia/Shanghai"))

  def parseCsv(tokens: Seq[String]): Fill = {
    val fillId = tokens.head.toLong
    val id = tokens(1).toLong
    val timeString: String = tokens(2)
    val btcBalance = SafeDouble(tokens(5).split("/")(0).replaceAll(",", "").toDouble)
    val cnyBalance = SafeDouble(tokens(7).replaceAll(",", "").toDouble)
    val parsedTime: TemporalAccessor = oft.parse(timeString)
    val ts = Instant.from(parsedTime)
    try {
      val btcStr = tokens(4).replaceAll(",", "")
      val btcS = if (btcStr.startsWith("+")) btcStr.substring(1) else btcStr
      val cnyStr = tokens(6).replaceAll(",", "")
      val cnyS = if (cnyStr.startsWith("+")) cnyStr.substring(1) else cnyStr
      val btc: Monetary = Monetary(SafeDouble(btcS.toDouble), Bitcoin)
      val cny: Monetary = Monetary(SafeDouble(cnyS.toDouble), Currency("CNY"))
      val position = Position(btc, cny, None, ts.toEpochMilli, UUID.randomUUID())
      val inventory: Inventory = Map(Bitcoin -> btcBalance, Currency("CNY") -> cnyBalance)
      Fill(Party("OKCN"), position, id.toString, Some(fillId.toString), Some(inventory))
    } catch {
      case x: Throwable =>
        x.printStackTrace()
        throw x
    }

  }

  def loadFromCsv(dir: File): Array[Fill] = {
    val files = dir.listFiles(new FilenameFilter {
      override def accept(dir: File, name: String): Boolean = name.endsWith(".csv")
    })
    val data = for (file <- files) yield {
      val reader = CSVReader.open(file)
      val fills = reader.toStream.drop(1).filter(line => {
        !line.head.startsWith("1024548491")
      }).map(parseCsv).toArray
      reader.close()
      fills
    }
    data.flatten
  }
} 
开发者ID:atemerev,项目名称:aggro,代码行数:62,代码来源:CsvLoader.scala


示例6: TimeSeriesParser

//设置package包名称以及导入依赖的类
package funchisqwrapper.parsing

import java.io.File

import com.github.tototoshi.csv.{CSVReader, TSVFormat}
import funchisqwrapper.TimeSeriesTypes.{ContinuousProfile, ContinuousTimeSeries}

object TimeSeriesParser {
  private val ID_LABEL = "id"

  def parse(f: File): ContinuousTimeSeries = {
    val format = new TSVFormat {}
    val reader = CSVReader.open(f)(format)
    val (headers, rows) = reader.allWithOrderedHeaders()

    assert(headers.size > 1)
    assert(headers.head == ID_LABEL)

    val labels = headers.tail

    val profiles = rows map { row =>
      val id = row(ID_LABEL)
      val values = labels map { l => row(l).toDouble }
      ContinuousProfile(id, values)
    }

    ContinuousTimeSeries(profiles)
  }
} 
开发者ID:koksal,项目名称:funchisq-wrapper,代码行数:30,代码来源:TimeSeriesParser.scala


示例7: FileSystem

//设置package包名称以及导入依赖的类
package services

import java.io.File
import javax.inject._

import com.github.tototoshi.csv.{CSVReader, DefaultCSVFormat}
import play.api.{Configuration, Logger}


@Singleton
class FileSystem @Inject() (configuration: Configuration) {

  val repodir = configuration.underlying.getString("importnow.dir")
  val uploadsDir = new File(repodir + "/uploads")
  uploadsDir.mkdirs()
  Logger.info(s"Saving upload files to $uploadsDir")

  def getUploads: List[String] = {
    uploadsDir.list().toList.sorted
  }

  def uploadFile(filename: String, contentType: Option[String])(writeTo: File => Unit): Unit = {
    val file = new File(uploadsDir, filename)
    writeTo(file)
  }

  def fileHeaders(filename: String): Option[List[String]] = {
    withCsvReader(filename) {
      reader => reader.readNext()
    }
  }

  def fileStream[T](filename: String)(streamFunction: Stream[Map[String, String]] => T): T = {
    withCsvReader(filename) {
      reader => streamFunction(reader.toStreamWithHeaders)
    }
  }

  private def withCsvReader[T](filename: String)(function: CSVReader => T): T = {
    val file = new File(uploadsDir, filename)

    implicit object MyFormat extends DefaultCSVFormat {
      override val delimiter = ';'
    }

    val reader = CSVReader.open(file)
    val result = try {
      function(reader)
    } finally {
      reader.close
    }
    result
  }

} 
开发者ID:jkransen,项目名称:importnow,代码行数:56,代码来源:FileSystem.scala


示例8: AnomalyApp

//设置package包名称以及导入依赖的类
package com.carl.ts.examples

import java.io.File
import java.time.LocalDateTime

import com.github.tototoshi.csv.CSVReader
import com.carl.ts.Series
import com.carl.ts.Stats.SeriesStats



object AnomalyApp {

  def main(args: Array[String]): Unit = {
    val series = loadCSV("testdata/anomalies.csv")
    printStats("Raw data", series)
    printStats("Differences", series.differentiate)
  }

  def loadCSV(fileName: String): Series[Double] = {
    import java.time.format.DateTimeFormatter
    val format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
    val reader = CSVReader.open(new File(fileName))
    val data = reader.allWithHeaders()
    val readings: List[(LocalDateTime, Double)] = data.map(row => {
      val index = row.get("time").map(x => LocalDateTime.parse(x,format)).getOrElse(LocalDateTime.now())
      val value = row.get("velocity").map(_.toDouble).getOrElse(0.0)
      (index, value)
    })
    new Series(readings)
  }

  def printStats(title: String, series: Series[Double]) = {
    println(title)
    println("  * Length: " + series.length)
    println("  * Mean: " + series.mean)
    println("  * Std dev: " + series.stddev)

  }
} 
开发者ID:klangner,项目名称:timeseries-scala,代码行数:41,代码来源:AnomalyApp.scala


示例9: VolunteerCSVReader

//设置package包名称以及导入依赖的类
package Readers

import java.io.File
import java.time.Month

import Core.{Availability, Person}
import com.github.tototoshi.csv.CSVReader

case class VolunteerCSVReader(filename: String) {
  private val reader = CSVReader.open(new File(filename))
  private val raw: List[Map[String, String]] = reader.allWithHeaders()

  private def availability(row: Map[String,String], tentativeMeansAvailable: Boolean) : Availability = {
    val sun = "Available Sun 6/12"    -> Availability.AllDay(2016, Month.JUNE, 12)
    val mon = "Available Mon 6/13"    -> Availability.AllDay(2016, Month.JUNE, 13)
    val tue = "Available Tue 6/14"    -> Availability.AllDay(2016, Month.JUNE, 14)
    val wed = "Available Wed 6/15"    -> Availability.AllDay(2016, Month.JUNE, 15)
    val thu = "Available Thurs 6/16"  -> Availability.AllDay(2016, Month.JUNE, 16)
    val fri = "Available Fri 6/17"    -> Availability.AllDay(2016, Month.JUNE, 17)

    val days = List(sun, mon, tue, wed, thu, fri)

    // compute availability
    days.foldLeft(Availability.NotAvailable){ case (acc,(key,avail)) =>
        if (row(key) == "Y"
            || (if (tentativeMeansAvailable) { row(key) == "T" } else { false })) {
          acc + avail
        } else {
          acc
        }
    }
  }

  def people(tentativeMeansAvailable: Boolean) : (Set[Person],Set[Person]) = {
    val allPeople = raw.flatMap { row =>
      if (row("Can Serve (confirmed)") == "Y") {
        val avail = availability(row, tentativeMeansAvailable)

        Some(
          Person(
            row("First name"),
            row("Last name"),
            avail
          )
        )
      } else {
        None
      }
    }.toSet

    allPeople.partition { person => person.availability != Availability.NotAvailable }
  }
} 
开发者ID:dbarowy,项目名称:schedulous,代码行数:54,代码来源:VolunteerCSVReader.scala



注:本文中的com.github.tototoshi.csv.CSVReader类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala CSVFormat类代码示例发布时间:2022-05-23
下一篇:
Scala Cursor类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap