#MonthOfJulia Day 9: Input/Output

By: Andrew Collier

Re-posted from: http://www.exegetic.biz/blog/2015/09/monthofjulia-day-09-input-and-output/

Your code won’t be terribly interesting without ways of getting data in and out. Ways to do that with Julia will be the subject of today’s post.

Julia-Logo-IO

Console IO

Direct output to the Julia terminal is done via print() and println(), where the latter appends a newline to the output.

julia> print(3, " blind "); print("mice!n")
3 blind mice!
julia> println("Hello World!")
Hello World!

Terminal input is something that I never do, but it’s certainly possible. readline() will read keyboard input until the first newline.

julia> response = readline();
Yo!
julia> response
"Yo!n"

Reading and Writing with a Stream

Writing to a file is pretty standard. Below we create a suitable name for a temporary file, open a stream to that file, write some text to the stream and then close it.

filename = tempname()
fid = open(filename, "w")
write(fid, "Some temporary text...")
close(fid)

print() and println() can also be used in the same way as write() for sending data to a stream. STDIN, STDOUT and STDERR are three predefined constants for standard console streams.

There are various approaches to reading data from files. One of which would be to use code similar to the example above. Another would be to do something like this (I’ve truncated the output because it really is not too interesting after a few lines):

julia> open("/etc/passwd") do fid
           readlines(fid)
       end
46-element Array{Union(UTF8String,ASCIIString),1}:
 "root:x:0:0:root:/root:/bin/bashn"                                               
 "daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologinn"                               
 "bin:x:2:2:bin:/bin:/usr/sbin/nologinn"                                          
 "sys:x:3:3:sys:/dev:/usr/sbin/nologinn"                                          
 "sync:x:4:65534:sync:/bin:/bin/syncn"    

Here readlines() returns the entire contents of the file as an array, where each element corresponds to a line of content. readall() would return everything in a single string. A somewhat different approach would be to use eachline() which creates an iterator allowing you to process each line of the file individually.

Delimited Files

Data can be read from a delimited file using readdlm(), where the delimiter is specified explicitly. For a simple Comma Separated Value (CSV) file it’s more direct to simply use readcsv().

julia> passwd = readdlm("/etc/passwd", ':');
julia> passwd[1,:]
1x7 Array{Any,2}:
 "root"  "x"  0.0  0.0  "root"  "/root"  "/bin/bash"

The analogues writedlm() and writcecsv() are used for writing delimited data.

These functions will be essential if you are going to use Julia for data analyses. There is also functionality for reading and writing data in a variety of other formats like xls and xlsx, HDF5, Matlab and Numpy data files and WAV audio files.

File Manipulation

Julia implements a full range of file manipulation methods (documented here), most of which have names similar to their UNIX counterparts.

A few other details of my dalliance with Julia’s input/output functionality can be found on github.

The post #MonthOfJulia Day 9: Input/Output appeared first on Exegetic Analytics.