top of page

Julia Lang: Reading in Array From .txt File

One challenge I had to overcome fairly quickly as I moved to complete my Algorithm course assignments in Julia was a need to read in data from .txt files, as this is often how our assignments are structured. Here I will briefly walk though the basics of reading in text data from a file. You can get more information in the Julia Documentation.


READ IN FILES


I wrote a basic function to read in my files. In this example I'm not passing an argument, such as a file path, but that modification could certainly be added.


First, I include the 'DelimitedFiles' package, by calling:


using Delimited Files


Then, opening the function begins by calling the open function. Here I use the flag "r" for read-only; I don't want to be able to change the contents of this original file. The open function outputs a string stream, so I decided to name this "stream".


stream = open("/home/katrina/Documents/CSCI3412_Algorithms/Test.txt", "r")


Next, I call the readline function that will read one line at a time from my data stream. In the case of the text files I am reading, they are of the format:


Line 1: Title

Line 2: <number_of_elements>

Line 3: n1

Line 4: n2

Line 5: n3

...

Line n: n


So in order to obtain the title and the number of elements in the array in the text file, I call the readline function two times, and store the return from the function to string variables. I use the flag "keep=false" so that the data stream basically disregards those lines going forward so you don't read them in twice.


title = readline(stream, keep=false) numIndicesString = readline(stream, keep=false)


As I mentioned above, these first two lines from the string stream were stored in string variables - but I need the number_of_elements line as a numerical value. To do that, we can use the function parse.


numIndices = parse(Int64, numIndicesString)


Now that I have the information I need from the first two lines, I can create an array of the size I need to copy that data from the text file into the array. I also created a variable called index and set it equal to 1, it will be used to index the remaining lines of data into my array.


array = Array{Int64}(undef, numIndices, 1)

index = 1


Now, using a while loop, while we are not at the end of the file, read each line into the array and increase the index for each new line. Though I kept the flag "keep=true" instead of false, I believe the false flag would work just as well.


while eof(stream) != true

tmp = readline(stream, keep=true)

array[index] = parse(Float64, tmp)

index += 1

end


That is the end of the read_in_file function. Next, we can invoke the function to execute it:


listToSort = read_in_file()


If your array isn't too long you can use the following lines to print your array out to verify to yourself that your data printed our effectively..


for i = 1:length(listToSort)

# println(listToSort[i])

end




WRITE OUT FILES


Say, perhaps, that you processed your array in some capacity and want to store your output beyond printing it to the console. You can easily write out this array to an output text file. This is extremely helpful for very large outputs as well. Invoke the writedlm funciton and pass the name of a file you either have access to or the name of a new file that the program will have access to write into.


writedlm("sortResults.txt", listToSort)




FULL CODE


Below is the code in its entirety. You can also reference my GIthub repository: http://github.com/siegfrkn/JuliaDemoScripts/blob/master/ReadInFile.jl



#!/usr/bin/julia


# Julia Lang


# Written by Katrina Siegfried

# 2018-09-15

# CSCI3412_Algorithms


# This file reads in a txt file contianing an array that has the following format

# Line 1 Title of txt file

# Line 2 <number of elements>

# Line 3 n1

# Line 4 n2

# Line 5 n3

# ...

# Line n n


using DelimitedFiles


function read_in_file()

# open the input file, save the first two lines as title and number of indices

stream = open("/home/katrina/Documents/CSCI3412_Algorithms/Test.txt", "r")

# stream = open("/home/katrina/Documents/CSCI3412_Algorithms/one-million-randoms.txt", "r")

# stream = open("/home/katrina/Documents/CSCI3412_Algorithms/nearly-sorted.txt", "r")

title = readline(stream, keep=false)

numIndicesString = readline(stream, keep=false)

numIndices = parse(Int64, numIndicesString)

# create an empty array of length passed in the file as numIndices

array = Array{Int64}(undef, numIndices, 1)

index = 1

# while not at the end of the file, read in all the remaining values as Int64

while eof(stream) != true

tmp = readline(stream, keep=true)

array[index] = parse(Float64, tmp)

index += 1

end

# return the array from the file for sorting

return array

end


# CALL FUNCTION TO READ IN FILE

listToSort = read_in_file()


# UNCOMMENT CODE BELOW TO PRINT UNSORTED LIST

for i = 1:length(listToSort)

println(listToSort[i])

end


# WRITE RESULTS TO NEW TXT FILE

writedlm("sortResults.txt", listToSort)





Featured Posts
Recent Posts
Archive
Search By Tags
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square
bottom of page