# Read complex text files in Julia

**URL:** <https://discourse.julialang.org/t/read-complex-text-files-in-julia/57386>\
**Category:** Data\
**Created:** [March 17, 2021, 2:07pm UTC](https://discourse.julialang.org/t/read-complex-text-files-in-julia/57386 "2021-03-17T14:07:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![baprata](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baprata/32/204269_2.png) [@baprata](https://discourse.julialang.org/u/baprata)\
**Post date:** [March 17, 2021, 2:07pm UTC](https://discourse.julialang.org/t/read-complex-text-files-in-julia/57386/1 "2021-03-17T14:07:45Z")

</div>

I need to read this file ([https://drive.google.com/file/d/1VSVxE7tMFutzNZhVmhsFnoSgCnpFZAUL/view?usp=sharing](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

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 17, 2021, 2:19pm UTC](https://discourse.julialang.org/t/read-complex-text-files-in-julia/57386/2 "2021-03-17T14:19:09Z")

</div>

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

```julia
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.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 17, 2021, 2:26pm UTC](https://discourse.julialang.org/t/read-complex-text-files-in-julia/57386/3 "2021-03-17T14:26:48Z")

</div>

Once you read the header line that specifies the sizes, I think you can use the [`readdlm` function](https://docs.julialang.org/en/v1/stdlib/DelimitedFiles/#DelimitedFiles.readdlm-Tuple%7BAny,AbstractChar,Type,AbstractChar%7D) to read each array, passing a `dims` parameter to specify the size of each array to read in.

---

<div class="post-metadata">

**Author:** ![baprata](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baprata/32/204269_2.png) [@baprata](https://discourse.julialang.org/u/baprata)\
**Post date:** [March 17, 2021, 4:13pm UTC](https://discourse.julialang.org/t/read-complex-text-files-in-julia/57386/4 "2021-03-17T16:13:27Z")

</div>

Dear Leandro,  
Thank you very much.  
Bruno

---

<div class="post-metadata">

**Author:** ![baprata](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baprata/32/204269_2.png) [@baprata](https://discourse.julialang.org/u/baprata)\
**Post date:** [March 17, 2021, 4:13pm UTC](https://discourse.julialang.org/t/read-complex-text-files-in-julia/57386/5 "2021-03-17T16:13:50Z")

</div>

Dear Steven,  
Thank you very much.  
Bruno
