Read complex text files in Julia

I need to read this file (https://drive.google.com/file/d/1VSVxE7tMFutzNZhVmhsFnoSgCnpFZAUL/view?usp=sharing) to obtain a matrix and a vector as follows. Could anybody helps me?

Array (20 x 5):

1 4 87 21 28
68 32 17 38 43
9 48 8 85 6
30 92 37 78 33
70 85 72 31 17
33 47 25 83 28
49 15 88 29 78
98 50 89 83 3
15 15 51 3 60
1 78 66 78 71
56 21 69 60 96
65 100 25 68 30
9 78 50 89 51
58 96 69 34 1
71 99 78 75 20
15 92 79 59 87
69 29 10 63 29
88 17 28 55 97
18 16 27 18 58
50 29 16 61 74

Vector (20 x 1):
1038
905
905
901
911
1048
1050
965
1011
976
1037
988
957
1052
1052
918
908
903
901
928

I think you need to write your parser for this file. The key commands needed are…

function read_my_file(filename)
  file = open(filename,"r")
  for line in eachline(file)
      data = split(line) # produces a vector of strings
     n = parse(Int,data[1]) # converts one of the data fields to a number
     m = Matrix{Int}(undef,n1,n2) # allocates the matrix, where n1=20,n=5 in the example
     v = Vector{Int}(undef,n) # allocate the vector
     if data[1] == "NW"
       #read the vector data here
     end 
  end
  close(file)
  return m, v
end

with proper care, a combination of those things can do the job. But since the file has a specific format with different number of columns, unless someone implemented its parsing already, it has to be done by hand.

2 Likes

Once you read the header line that specifies the sizes, I think you can use the readdlm function to read each array, passing a dims parameter to specify the size of each array to read in.

3 Likes

Dear Leandro,
Thank you very much.
Bruno

Dear Steven,
Thank you very much.
Bruno