Importing data from Files in Julia

25 Aug, 2020
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.
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 PkgPkg.add("CSV")Pkg.add("DataFrames")  using CSVforo = CSV.read("Records1.csv")
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")
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 filereadlines(a)
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 pkgusing JSON  # Loading data using parsefile()JSON.parsefile("/Users/mridul/Desktop/Learning Julia/jfie.json")
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 pkgPkg.add("ZipFile") using ZipFile  # storing and loadingr = ZipFile.Reader("forwork.txt.zip") for i in r.files    x_zip = readlines(i)    print(x_zip)end
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 mannerread_xml = parse_file("sample.xml")   # reading and loading the file in formal mannerbase = root(read_xml)
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 pkgusing HDF5   # opening the fileNew_HDF = h5open("AHdf5.h5")  read(New_HDF) # reading the 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 connectiona = Gumbo.open("New.html") readlines(a)  # closing the connection close(a)
 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 Queryversedf = load("Records1.csv") |> DataFramedf |> save("mydata.feather")  # passing the saved feather filedf1 = 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 loadingdf1 = load("co3.dta") |> DataFrame   # for spss file loadingdf1 = load("experim.sav") |> DataFrame    # for sas file loadingdf2 = 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 pathpu_img_path = "/Users/mridul/Desktop/Learning Julia/Butcher.png"   # Passing the stored value to load the imageimg = load(pu_img_path)
julia-FileHandling
Julia
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.