lenzinho
lenzinho

Reputation: 401

Line-by-line parsing of text file containing data with Julia?

I'm trying to read tensor elements written to a text file. The first line of the file defines the tensor dimension. The next lines give the tensor values. In Matlab syntax, I was able to achieve this with the following line of code, but I am having a difficult time coding an equivalent function in Julia. Any help is greatly appreciated.

fid=fopen(fname);
    shape = sscanf(fgetl(fid),'%i');
    for j = 1:shape(3)
    for i = 1:shape(1)
        A(i,:,j) = str2num(fgets(fid));
    end
    end
fclose(fid);

The first lines of a typical file are reproduced below:

         4           4          48
1.00000  0.00000  0.00000  0.00000
0.00000  1.00000  0.00000  0.00000
0.00000  0.00000  1.00000  0.00000
0.00000  0.00000  0.00000  1.00000
-1.00000  0.00000   0.00000  0.00000
0.00000  1.00000   0.00000  0.00000
0.00000  0.00000  -1.00000  0.00000
0.00000  0.00000   0.00000  1.00000
-1.00000   0.00000  0.00000  0.00000
...

Upvotes: 4

Views: 2031

Answers (2)

daycaster
daycaster

Reputation: 2697

As @colin said in his comment, such a file can be easily read into Julia with this:

julia> data, heading = readdlm("/tmp/data.txt",  header=true)
(
9x4 Array{Float64,2}:
  1.0  0.0   0.0  0.0
  0.0  1.0   0.0  0.0
  0.0  0.0   1.0  0.0
  0.0  0.0   0.0  1.0
 -1.0  0.0   0.0  0.0
  0.0  1.0   0.0  0.0
  0.0  0.0  -1.0  0.0
  0.0  0.0   0.0  1.0
 -1.0  0.0   0.0  0.0,

1x4 Array{AbstractString,2}:
 "4"  "4"  "48"  "")

The two values returned are the array of Float64s and the header row as an array of strings.

Any use?

Upvotes: 6

Michael Ohlrogge
Michael Ohlrogge

Reputation: 10980

If you do want to read line by line, you can use the following:

a = open("/path/to/data.txt", "r")
for line in eachline(a)
    print(line) ## or whatever else you want to do with the line.
end
close(a)

In particular, a syntax like this:

LineArray = split(replace(line, "\n", ""), "\t")

Might be useful to you. It will (a) remove the line break at the end of the line and (b) then split it up into an indexed array so that you can then pull elements out of it based on predictable positions they occupy in the line.

You could also put:

Header = readline(a);

right after you open the file if you want to specifically pull out the header, and then run the above loop. Alternatively, you could use enumerate() over eachline(a) and then perform logic on the index of the enumeration (e.g. define the header when the index = 1).

Note though that this will be slower than the answer from daycaster, so it's only worthwhile if you really need the extra flexibility.

Upvotes: 5

Related Questions