Open In App

Importing data from Files in Julia

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:




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



Importing data from Excel files

Approach:




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

Importing data from Text files

Approach:




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

Importing data from JSON files

Approach:




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:




# 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:




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:




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:




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:




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:




# 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:




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)


Article Tags :