{"id":3502,"date":"2016-03-27T23:54:19","date_gmt":"2016-03-27T23:54:19","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3502"},"modified":"2016-03-27T23:54:19","modified_gmt":"2016-03-27T23:54:19","slug":"write-csv-files-scala","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/write-csv-files-scala\/","title":{"rendered":"Write CSV files in Scala"},"content":{"rendered":"<p>There is a convenient library for reading\/writing CSVs in Scala.<\/p>\n<p>To include it in your project, add this to your build.sbt:<\/p>\n<pre lang=\"scala\">\nlibraryDependencies += \"com.github.tototoshi\" %% \"scala-csv\" % \"1.3.0\"\n<\/pre>\n<p>And import it:<\/p>\n<pre lang=\"scala\">\nimport com.github.tototoshi.csv._ \n<\/pre>\n<p>Then say you create some data in a loop:<\/p>\n<pre lang=\"scala\">\nresults +=\n  List(\n    domain,\n    conn.getCipherSuite,\n    cert.hashCode,\n    cert.getPublicKey().getAlgorithm,\n    cert.getPublicKey().getFormat,\n    cert.getSigAlgName,\n    cert.getSigAlgOID,\n    cert.getIssuerDN,\n    cert.getSerialNumber,\n    cert.getSubjectDN,\n    cert.getVersion,\n    cert.getNotAfter,\n    cert.getNotBefore,\n    cert.getPublicKey.getFormat,\n    cert.getPublicKey.getAlgorithm,\n    cert.getIssuerDN.getName\n  )\n  \nallData ++= data\n<\/pre>\n<p>Then write to a file:<\/p>\n<pre>\nval f = new File(\"out.csv\")\nval writer = CSVWriter.open(f)\nwriter.writeAll(allData)\n<\/pre>\n<p>This is an example of what it prints out (showing that it does escaping):<\/p>\n<pre>\nwww.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\"\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>There is a convenient library for reading\/writing CSVs in Scala. To include it in your project, add this to your build.sbt: libraryDependencies += &#8220;com.github.tototoshi&#8221; %% &#8220;scala-csv&#8221; % &#8220;1.3.0&#8221; 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, &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/write-csv-files-scala\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Write CSV files in Scala&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[480,482],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3502"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=3502"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3502\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3502"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3502"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3502"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}