Writing Data in Tabular form to Files in Julia

Julia is a high level, high performance, dynamic programming language which allows users to load, save, and manipulate data in various types of files for data science, analysis, and machine learning purposes. Tabular data is data that has a structure of a table and it can be easily written into various files like text, CSV, Excel, etc. 

To perform such operations on data and files with ease, we add the Queryverse.jl package which provides us ease of use for other useful packages such as Query.jl, FileIO.jl, CSVFiles.jl, etc.

Julia

filter_none

edit
close

play_arrow

link
brightness_4
code

# Adding the Queryverse package
using Pkg
Pkg.add("Queryverse")

chevron_right


Writing Tabular Data to Text Files

First, we create a text file using the touch() function.

Julia



filter_none

edit
close

play_arrow

link
brightness_4
code

# Creating a text file
touch("geek.txt")

chevron_right


And now we can open the file and write data into it as shown below.

Julia

filter_none

edit
close

play_arrow

link
brightness_4
code

# Writing data into a text file in a tabular format
# Opening the created file
f = open("geek.txt", "w")
  
# Writing data into text file
write(f, "A B C
       1 4 7
       2 5 8
       3 6 9")
         
# Closing text file
close(f)

chevron_right


 
Output:

Writing Tabular Data to CSV Files

DataFrames are used to store data in a tabular form and these DataFrames can be written into CSV or Excel files by using the Queryverse.jl package and the save() function. Queryverse.jl package lets the FileIO.jl package use the CSVFiles.jl package to implement this.

Julia

filter_none

edit
close

play_arrow

link
brightness_4
code

using Queryverse
  
# Ceating a DataFrame
df = DataFrame(subject =["Physics", "Chemistry", "English"], marks =[45, 62, 59])
  
# Writing a DataFrame into csv file
df |> save("marks.csv")
  
# can also be implemented as - save("marks.csv", df)

chevron_right


 
 



 We have successfully written tabular data into a  CSV file. The format of the data being written can be changed using various keywords as arguments to the save() function. 

delim keyword is used to separate columns in the file with a character that we can specify by equating it to the keyword.

Julia

filter_none

edit
close

play_arrow

link
brightness_4
code

# Separating comlumns with ';' 
df |> save("marks.csv", delim =';')

chevron_right


 
 

The column names of the DataFrame take up the first row of the file. To change this we can use the header keyword argument and equate it to false to remove the column names in the file.

Julia

filter_none

edit
close

play_arrow

link
brightness_4
code

# Creating a DataFrame
df = DataFrame(subject = ["Physics", "Chemistry", "English"], 
               marks = [45, 62, 59])
  
# Removing the column names from the first row of the file
df |> save("marks.csv", header = false)

chevron_right


 
 

 When the data is written into a file with the save() function, any text in the data will be represented between double quotes( ” ) and if there is a double quote in the text, a backlash ( \ ) is placed before it for differentiation. We can change these characters used for representation with the quotechar and the escapechar keyword arguments as shown below.



Julia

filter_none

edit
close

play_arrow

link
brightness_4
code

# Creating a DataFrame
df = DataFrame(subject =["Physics + Chemistry", "English"], marks =[107, 59])
  
# Changing separation characters in the file
df |> save("marks.csv", quotechar ='+', escapechar ='/')

chevron_right


 
 

 

Writing Tabular Data to Excel Sheets

The process for writing data into excel sheets is the same as that of CSV files, which has been discussed above, but we have to specify a file with the extension ‘*.xlsx’ instead of a ‘.csv’ in the save() function.

Julia

filter_none

edit
close

play_arrow

link
brightness_4
code

# Creating a DataFrame
df = DataFrame(subject = ["Maths", "Science", "Computers"], 
               marks = [61, 72, 49])
  
# Writing the DataFrame into an Excel file
df |> save("marks.xlsx")

chevron_right


 
 

 In the excel file, we can change the name of the sheet in which the data is being written into with the sheetname keyword argument as shown below.

Julia

filter_none

edit
close

play_arrow

link
brightness_4
code

# Changing name of the sheet in the excel file
df |> save("marks.xlsx", sheetname = "marks in excel")

chevron_right


 
 

In Julia, we can write tabular data not only in the three files discussed above also in various other files like Feather, Stata, SPSS, SAS, etc. The save function is applicable to all these file types. We just have to mention the respective extension (*.feature, *.dta, *.por, *.sav) to the file we want to write into.

 




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.