Storing Output on a File in Julia
Last Updated :
04 May, 2022
Julia provides a vast library to store and save the output in multiple file formats. We can store the output in various forms such as CSV(comma-separated value) or in Excel or just simply a text file.
Storing Data on Text File
Using open() function
In order to store the data in a text file, we need to use open() function with “w” flag which allows us to write output to a file. The open function takes two parameters first the filename and second the mode you need the file to open.
In order to store data in a text file, we need to use the .txt extension after the filename. Also if the file is not present it will create the file for you. The console prints the numbers of characters in the file.
Example:
Julia
ans = "Hello World"
open ( "geek.txt" , "w" ) do file
write( file , ans)
end
|
Using DelimitedFiles.writedlm() function
Another method to store data is to use DelimitedFiles.writedlm() function. The function takes two parameters the first is the filename and the second parameter is output. In order to use this function, we need to import DelimitedFiles.
Example:
Julia
using DelimitedFiles
numbers = rand( 5 )
writedlm( "geek.txt" , numbers)
|
Storing Data on CSV File
Using CSV.jl library
We can store data on CSV files by importing a library CSV.jl which provides various functions related to storing reading and writing output into a CSV file. It provides more control than other methods. We also need to install the DataFrame package.
In order to use this, we need to install the package. To install type the following in Julia console.
# importing the Pkg module
using Pkg
Pkg.add("CSV")
# installing the DataFrame
Pkg.add("DataFrames")
Now to store output we just need to import it.
Example:
Julia
using CSV, DataFrames
numbers = rand( 5 , 5 )
CSV.write( "geek.csv" , DataFrame(numbers),
header = false)
|
Using DelimitedFiles.writedlm() function
Another way to store data is to use DelimitedFiles.writedlm() function. The function takes two parameters the first is the filename and the second parameter is output. In order to use this function, we need to import DelimitedFiles. Here also file extension must be .csv.
Example:
Julia
using DelimitedFiles
numbers = rand( 5 , 5 )
writedlm( "geek.csv" , numbers, ", " )
|
Storing Data on Excel File
Using XLXS.jl library
We can store data on excel file by importing a library XLXS.jl which provides various functions related to storing reading and writing output into an excel file. It provides more control than other methods.
To use the package we first need to install the package. To install the package just write the following command in Julia terminal.
# importing the Pkg module
using Pkg
Pkg.add("XLSX")
Now to store the data we can use its various methods. We need to use the “w” flag to able to write output the xls file.
Example:
Julia
using XLSX
XLSX.openxlsx( "geek.xls" , mode = "w" ) do f
sheet = f[ 1 ]
XLSX.rename!(sheet, "new_sheet" )
sheet[ "A1" ] = "Welcome"
sheet[ "A2" ] = "to"
sheet[ "A3" ] = "GeeksForGeeks"
end
|
Using DelimitedFiles.writedlm() function
Another way to store data is to use DelimitedFiles.writedlm() function. The function takes two parameters the first is the filename and the second parameter is output. In order to use this function, we need to import DelimitedFiles. Here also file extension must be .xls.
Example:
Julia
using DelimitedFiles
numbers = rand( 5 , 5 )
writedlm( "geek.xls" , numbers)
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...