You can use the following template in order to import a CSV file into Julia:
using CSV CSV.read("The path where your CSV file is stored\\File Name.csv")
But just in case you were wondering, I’ll show you the full steps to import a CSV file into Julia.
Steps to Import a CSV File into Julia
Step 1: Install the CSV package
In order to install the CSV package, you’ll need to open the Julia command-line:
You’ll then see this display:
Type the following code in the command-line, and then press ENTER:
using Pkg
Finally, to complete the installation of the CSV package, type the code below, and then press ENTER:
Pkg.add("CSV")
It would take about a minute for the installation to complete.
Step 2: Save your data in a CSV file
Next, save your data in a CSV file.
For demonstration purposes, let’s say that you have the following data about Cars:
Brand | Price | Year |
Honda Civic | 22000 | 2015 |
Toyota Corolla | 25000 | 2013 |
Ford Focus | 27000 | 2018 |
Audi A4 | 35000 | 2018 |
You can copy that data into a CSV file, and then rename that file as Cars:
Step 3: Import the CSV file into Julia
In order to import the CSV file into Julia, you’ll need to use the template that you saw at the beginning of this guide:
using CSV CSV.read("The path where your CSV file is stored\\File Name.csv")
Here are few points to consider when importing your CSV file:
- Use double backslash within the path name to avoid any errors when importing your file
- At the end of the path, specify your file name + .csv. In our example, the file name is Cars, while the file type is csv. Therefore, the end of the path would be: Cars.csv
In my case, I stored the CSV file on my desktop, under this path:
C:\\Users\\Ron\\Desktop\\Cars.csv
So this is the code that I used to import the CSV file:
using CSV CSV.read("C:\\Users\\Ron\\Desktop\\Cars.csv")
I then ran the code in Juypter notebook (before you execute the code, make the necessary adjustments to your path name):
And this is the result:
As you can see, the output matches with the data that was stored in the CSV file.
Optionally, you can get the same results by applying the code in the Julia’s command-line:
You’ll then get the same data-set about Cars: