Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Julia Cookbook

You're reading from  Julia Cookbook

Product type Book
Published in Sep 2016
Publisher
ISBN-13 9781785882012
Pages 172 pages
Edition 1st Edition
Languages
Authors (2):
Raj R Jalem Raj R Jalem
Jalem Raj Rohit Jalem Raj Rohit
Profile icon Jalem Raj Rohit
View More author details

Handling data with TSV files


In this section, we will explain how to handle Tab Separated Values (TSV) files.

Getting ready

The DataFrames package is needed to deal with TSV files. So, as it is already installed as instructed in the previous section, we can move ahead and make sure that all the packages are up-to-date with the following command:

Pkg.update()

How to do it...

TSV files, as the name suggests, are files whose contents are separated by commas. TSV files can be accessed and read into the REPL process by the following method:

  1. Assign a variable to the local source directory of the file:

    s = "/Users/username/dir/data.tsv"
    
  2. The readtable() command is used to read the data from the source. The data is read in the form of a Julia DataFrame:

    data = readtable(s)
    

Data can be written to TSV files from a Julia DataFrame using the following steps:

  1. Create a data structure with some data inside it. For example, let's create a two-dimensional dataframe like the one we created in the previous example:

    using DataFrames
    df = DataFrame(A = 1:10, B = 11:20)
    
  2. Now, the dataframe, which we created in Step 1, can be exported to an external TSV file using the following command:

    writetable("data.csv",df)
    

The writetable() command is clever enough to make out the format of the file from the filename extension.

You have been reading a chapter from
Julia Cookbook
Published in: Sep 2016 Publisher: ISBN-13: 9781785882012
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}