Open In App

Importing data from Files in Julia

Improve
Improve
Like Article
Like
Save
Share
Report

Julia supports File Handling in a much easier way as compared to other programming languages. Various file formats can easily be loaded in our Julia IDE. Most of the file extension packages are loaded into the package, named Pkg in Julia. This basically adds the package needed to load data of different file formats.

The method which is available to import data from the file is add(). This method is in the Pkg object and different arguments are passed such as CSV, XLSX, DataFrames, etc.

Importing data from the CSV files

Approach:

  • Load the package Pkg
  • Now use the add() function Pkg.add()
  • Now pass the CSV in inverted commas as argument in the function
  • Now use read() function which read the whole CSV file.

Julia




using Pkg
Pkg.add("CSV")
Pkg.add("DataFrames")
  
using CSV
foro = CSV.read("Records1.csv")


Importing data from Excel files

Approach:

  • Load the package Pkg
  • Now use the add() function Pkg.add()
  • Now pass the ExcelReaders in inverted commas as argument in the function
  • Now use function which read the whole XLSX file.

Julia




Pkg.add("ExcelReaders")
using ExcelReaders
  
# dataframe for reading and storing the data.
df1 = readxlsheet("sample1.xlsx", "Sheet1")


Importing data from Text files

Approach:

  • First, open the file using open() function and pass the file.txt as an argument in it.
  • This function will return the lines and which can be stored in a variable which would connect the file with variable present on your local disk.
  • Then apply the function readlines() and pass the variable, this function actually reads all the lines inside the file and return the text of it.
  • Now close the connection of file with the disk using close() function and passing the variable in it.

Julia




a = open("forwork.txt")
  
# function to read lines of the file
readlines(a)


Importing data from JSON files

Approach:

  • First import the package named JSON with help of Pkg.add() function passing JSON as argument
  • Then applying and loading the package.
  • Then using the parsefile() function of this package which takes the path and resolves the path to reach the target file and returns the output.

Julia




Pkg.add("JSON") # Adding pkg
using JSON
  
# Loading data using parsefile()
JSON.parsefile("/Users/mridul/Desktop/Learning Julia/jfie.json")


Importing data from Zip files

Approach:

  • First add the package ZipFile by passing it as an argument in Pkg.add() function
  • Then load the package
  • Storing the data of the file inside the variable using Reader function and passing the zipped file (formwork.txt.zip) as an argument
  • Traversing the file using the loop with printing the data inside it.

Julia




# Adding pkg
Pkg.add("ZipFile"
using ZipFile
  
# storing and loading
r = ZipFile.Reader("forwork.txt.zip"
for i in r.files
    x_zip = readlines(i)
    print(x_zip)
end


Importing data from the XML file

Approach:

  • First, the package LightXML needs to be passed as argument inside the add() function.
  • Then using the parse_file() function to access the file and connecting it to the disk
  • Now this returns the data inside the file and can be stored in a variable.
  • In this function pass the file name in inverted commas to access the file.

Julia




Pkg.add("LightXML")
using LightXML
  
# for reading the file in informal manner
read_xml = parse_file("sample.xml"
  
# reading and loading the file in formal manner
base = root(read_xml)


Importing data from HDF5 file

Approach:

  • Opening the file using h5open() function and passing the h5 file as argument.
  • Then simple read() function would easily read the file.

Julia




Pkg.add("HDF5")
  
# loading the pkg
using HDF5 
  
# opening the file
New_HDF = h5open("AHdf5.h5")
  
read(New_HDF) # reading the file


Importing data from HTML file

Approach:

  • First add the package Gumbo with help of add function
  • Then open the file using open() function and pass the .html as an argument in it.
  • This function will return the lines and which can be stored in a variable which would connect the file with the variable present on your local disk.
  • Then apply the function readlines() and pass the variable, this function actually reads all the lines inside the file and return the text of it.
  • Now close the connection of file with the disk using close() function and passing the variable in it.

Julia




using Gumbo
  
# opening and setting the connection
a = Gumbo.open("New.html"
readlines(a)
  
# closing the connection 
close(a)


Importing data from Tabular data files

 Approach for feather files:

  • First load the CSV file
  • Then save the CSV file as feather file for the same dataframe
  • Now to show the contents of your data as feather file load it with load() function passing the feather file as argument in it.

Julia




Pkg.add("Queryverse")
using Queryverse
df = load("Records1.csv") |> DataFrame
df |> save("mydata.feather")
  
# passing the saved feather file
df1 = load("mydata.feather") |> DataFrame


Importing from data files of SAS, SPSS AND Stata

  Approach:

  • Use the load function and pass the file format as an argument.

Julia




# for stata file loading
df1 = load("co3.dta") |> DataFrame 
  
# for spss file loading
df1 = load("experim.sav") |> DataFrame  
  
# for sas file loading
df2 = load("meat.sas7bdat") |> DataFrame


Importing image files in Julia

   Approach:

  • First, add the package Images and FileIO by passing in the add() function as arguments
  • Loading these packages
  • Store the path of that image in the variable
  • Now apply the load function and pass the stored variable to get the path

Julia




Pkg.add("Images")
using Images, FileIO
  
# Loading the path
pu_img_path = "/Users/mridul/Desktop/Learning Julia/Butcher.png" 
  
# Passing the stored value to load the image
img = load(pu_img_path)




Last Updated : 25 Aug, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads