Write CSV files in Scala

There is a convenient library for reading/writing CSVs in Scala.

To include it in your project, add this to your build.sbt:

libraryDependencies += "com.github.tototoshi" %% "scala-csv" % "1.3.0"

And import it:

import com.github.tototoshi.csv._ 

Then say you create some data in a loop:

results +=
  List(
    domain,
    conn.getCipherSuite,
    cert.hashCode,
    cert.getPublicKey().getAlgorithm,
    cert.getPublicKey().getFormat,
    cert.getSigAlgName,
    cert.getSigAlgOID,
    cert.getIssuerDN,
    cert.getSerialNumber,
    cert.getSubjectDN,
    cert.getVersion,
    cert.getNotAfter,
    cert.getNotBefore,
    cert.getPublicKey.getFormat,
    cert.getPublicKey.getAlgorithm,
    cert.getIssuerDN.getName
  )
  
allData ++= data

Then write to a file:

val f = new File("out.csv")
val writer = CSVWriter.open(f)
writer.writeAll(allData)

This is an example of what it prints out (showing that it does escaping):

www.godaddy.com,TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,13751580,RSA,X.509,SHA256withRSA,1.2.840.113549.1.1.11,"CN=Go Daddy Root Certificate Authority - G2, O=""GoDaddy.com, Inc."", L=Scottsdale, ST=Arizona, C=US",7,"CN=Go Daddy Secure Certificate Authority - G2, OU=http://certs.godaddy.com/repository/, O=""GoDaddy.com, Inc."", L=Scottsdale, ST=Arizona, C=US",2,Sat May 03 03:00:00 EDT 2031,Tue May 03 03:00:00 EDT 2011,X.509,RSA,"CN=Go Daddy Root Certificate Authority - G2, O=""GoDaddy.com, Inc."", L=Scottsdale, ST=Arizona, C=US"

Leave a Reply

Your email address will not be published. Required fields are marked *