How to Read and Write CSV File in Scala?
Last Updated :
11 Jun, 2024
Data processing and analysis in Scala mostly require dealing with CSV (Comma Separated Values) files. CSV is a simple file format used to store tabular data, such as a spreadsheet or database. Each line of a CSV file is plain text, representing a data row, with values separated by commas (,). Reading from and writing to CSV files are common tasks across several programming situations. This article focuses on discussing steps to read and write CSV files in Scala.
Setting Up the Environment
To work with CSV files in Scala, you need to set up your development environment. Ensure you have Scala installed and a build tool like SBT (Scala Build Tool).
1. Create a new project using SBT:
sbt new scala/scala-seed.g8
2. Add necessary dependencies in the build.sbt:
Add the scala-csv library dependency in your build.sbt file.
libraryDependencies += "com.github.tototoshi" %% "scala-csv" % "1.3.10"
It'll look like,
import Dependencies._
ThisBuild / scalaVersion := "2.13.12"
ThisBuild / version := "0.1.0-SNAPSHOT"
ThisBuild / organization := "com.example"
ThisBuild / organizationName := "example"
lazy val root = (project in file("."))
.settings(
name := "CsvWork",
libraryDependencies += munit % Test,
libraryDependencies += "com.github.tototoshi" %% "scala-csv" % "1.3.10"
)
// See https://www.scala-sbt.org/1.x/docs/Using-Sonatype.html for instructions on how to publish to Sonatype.
Using Java.io.PrintWriter Class
Writing a CSV File
Data will be written to a CSV file using the java.io.PrintWriter class. The data will be written row by row, with each field separated by a comma. The data will be in form of strings and first row will be the header of the data.
Functions:
- new PrintWriter(new File(filename)): Opens or creates a file for writing.
- writer.println(data): Writes a line of data to the file.
Below is the Scala program to write CSV File:
Scala
import java.io.PrintWriter
object WriterExample1 {
def main(args: Array[String]): Unit = {
val filename = "outputs.csv"
val writer = new PrintWriter(filename)
writer.println("Name, Age, City")
writer.println("John, 30, New York")
writer.println("Alice, 25, London")
writer.close()
}
}
Explanation:
- Import important libraries: Scala's java.io.PrintWriter class can be used to write to files.
- Create the CSV file: Use new PrintWriter(new File("output.csv")) to open or create a new CSV file.
- Write data to the file: Use println or write methods to write data to the file.
Output:
Write CSV File in ScalaReading CSV In Scala
We will read a CSV file using scala.io.Source. The file will be read line by line, and each line will be sperate into fields using the comma.
Functions:
- Source.fromFile(filename): Opens the file for reading.
- file.getLines(): Reads the file line by line.
- line.split(delimiter): Splits each line into fields based on the delimiter.
Below is the Scala program to read a CSV File:
Scala
import scala.io.Source
object ReaderExample1 {
def main(args: Array[String]): Unit = {
val filename = "output.csv"
val delimiter = ","
val file = Source.fromFile(filename)
for (line <- file.getLines()) {
val fields = line.split(delimiter).map(_.trim)
println(fields.mkString(", "))
}
file.close()
}
}
Explanation:
- Import the required libraries: In Scala, you can use the scala.io.Source library to read files.
- Open CSV file: Use Source.fromFile("output.csv") to open the CSV file.
- Read the data: Split each line using the delimiter and process the data as needed.
Output:
Read CSV File in ScalaUsing Scala-csv Library
Writing CSV in Scala
Data will be written to a CSV file using the scala-csv library. The data will be in the form of a list of maps with each map representing individual rows. The headers, taken from the keys in the first map, shall precede all other records on file.
Approach:
- Import the necessary libraries.
- Open the CSV file using CSVWriter.
- Define the data as a list of maps.
- Extract headers from the data.
- Convert the data to a sequence of sequences.
- Write the headers and data to the CSV file.
- Close the writer.
Below is the Scala program to write CSV file:
Scala
import java.io.File
import com.github.tototoshi.csv._
object WriterExample2 {
def main(args: Array[String]): Unit = {
val writer = CSVWriter.open(new File("output.csv"))
val data = List(
Map("Name" -> "John", "Age" -> "30", "Country" -> "USA"),
Map("Name" -> "Anna", "Age" -> "28", "Country" -> "UK")
)
val headers = data.head.keys.toSeq
val rows = data.map(_.values.toSeq)
writer.writeRow(headers)
writer.writeAll(rows)
writer.close()
}
}
Output:
Write CSV File in ScalaReading CSV in Scala
We will use the scala-csv library to read data from a CSV file and print it to the console. The data will be read as a list of maps where each map represents a row with column headers as keys.
Approach:
- Import the necessary libraries.
- Open the CSV file using CSVReader.
- Read all rows with headers using allWithHeaders().
- Print each row.
- Close the reader.
Below is the Scala program to read a CSV file:
Scala
import java.io.File
import com.github.tototoshi.csv._
object ReaderExample2 {
def main(args: Array[String]): Unit = {
val reader = CSVReader.open(new File("output.csv"))
val allRows = reader.allWithHeaders()
allRows.foreach(println)
reader.close()
}
}
Output:
Read CSV File in ScalaConclusion
The scala-csv library is an efficient way of reading and writing CSV files in Scala. By following the steps given above, it is easy to add the operations on CSV file into your Scala applications. This feature is important in ETL processes, data science and other domains that involve data manipulation.
Similar Reads
How to read and write JSON files in Scala?
Scala is frequently used for reading and writing JSON files in a variety of applications, particularly it includes data transmission. Table of Content Steps for reading JSON files in Scala:Steps for writing JSON files in Scala:Steps for reading JSON files in Scala:When reading JSON files in Scala we
3 min read
How to Read CSV File in Ruby?
It is common to have tabular data stored in CSV (Comma Separated Values) files. In Ruby, one can handle CSV files without much ado because the built-in libraries make it easy to do so. This article focuses on discussing the ways to read a CSV file in Ruby. Approach to Read CSV Files in Ruby?There ar
2 min read
How to read parquet file in Scala?
Scala has good support through Apache Spark for reading Parquet files, a columnar storage format. Below is a comprehensive guide to reading Parquet files in Scala: Setting Up Your EnvironmentFirst, to create a development environment with all necessary libs and frameworks, you must do the following.
3 min read
Reading and Writing CSV Files in Python
CSV (Comma Separated Values) format is one of the most widely used formats for storing and exchanging structured data between different applications, including databases and spreadsheets. CSV files store tabular data, where each data field is separated by a delimiter, typically a comma. Python provi
4 min read
Writing to CSV files in R
For Data Analysis sometimes creating CSV data file is required and do some operations on it as per our requirement. So, In this article we are going to learn that how to write data to CSV File using R Programming Language. To write to csv file write.csv() function is used. Syntax: write.csv(data, pa
1 min read
How to Import a CSV File into R ?
A CSV file is used to store contents in a tabular-like format, which is organized in the form of rows and columns. The column values in each row are separated by a delimiter string. The CSV files can be loaded into the working space and worked using both in-built methods and external package imports
3 min read
How to check if a file exists in Scala?
When working on software projects it's crucial to check if a file exists before you interact with it in any way such as reading, writing, or modifying it. This practice helps avoid issues that may arise from attempting to handle an existing file. Scala provides methods to perform this check for the
2 min read
How to go To and From Java Collections in Scala?
In their respective languages, Java collections and Scala collections are two distinct sets of data structures that might be frequently utilized. Although Java collections are a factor of the Java Standard Library, Scala collections are made expressly to combine with the useful programming features
2 min read
Add header to file created by write.csv in R
In R programming, the write.csv() function is used to save data frames as comma-separated values (CSV) files. When a CSV file is created using the write.csv() function, it does not contain a header by default. However, it is often useful to include a header row in the CSV file that contains the name
3 min read
How to Execute OS Commands in Scala?
Scala is a versatile programming language. It offers smooth approaches to running OS instructions, whether or not you want to deal with documents, automate system operations, or communicate with external gear. This article focuses on discussing ways to execute OS commands in Scala. PrerequisitesInst
2 min read