# How to read a textfile directly as \`Matrix{Char}\`

**URL:** <https://discourse.julialang.org/t/how-to-read-a-textfile-directly-as-matrix-char/107070>\
**Category:** General Usage\
**Tags:** question, io\
**Created:** [December 3, 2023, 7:35am UTC](https://discourse.julialang.org/t/how-to-read-a-textfile-directly-as-matrix-char/107070 "2023-12-03T07:35:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [December 3, 2023, 7:35am UTC](https://discourse.julialang.org/t/how-to-read-a-textfile-directly-as-matrix-char/107070/1 "2023-12-03T07:35:15Z")

</div>

it’s that time of the year again, say we have:

```julia
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..

```

in a file, and we want:

```julia
julia> mapreduce(permutedims∘collect, vcat, readlines("./a.txt"))
10×10 Matrix{Char}:
 '4' '6' '7' '.' '.' '1' '1' '4' '.' '.'
 '.' '.' '.' '*' '.' '.' '.' '.' '.' '.'
 '.' '.' '3' '5' '.' '.' '6' '3' '3' '.'
 '.' '.' '.' '.' '.' '.' '#' '.' '.' '.'
 '6' '1' '7' '*' '.' '.' '.' '.' '.' '.'
 '.' '.' '.' '.' '.' '+' '.' '5' '8' '.'
 '.' '.' '5' '9' '2' '.' '.' '.' '.' '.'
 '.' '.' '.' '.' '.' '.' '7' '5' '5' '.'
 '.' '.' '.' '$' '.' '*' '.' '.' '.' '.'
 '.' '6' '6' '4' '.' '5' '9' '8' '.' '.'

```

is there easier way to do this? I was hoping `DelimitedFiles` can split “everything” but I can’t get it to work

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [December 3, 2023, 7:56am UTC](https://discourse.julialang.org/t/how-to-read-a-textfile-directly-as-matrix-char/107070/2 "2023-12-03T07:56:50Z")

</div>

```julia
julia> stack(collect.(readlines(b)); dims=1)
10×10 Matrix{Char}:
 '4' '6' '7' '.' '.' '1' '1' '4' '.' '.'
 '.' '.' '.' '*' '.' '.' '.' '.' '.' '.'
 '.' '.' '3' '5' '.' '.' '6' '3' '3' '.'
 '.' '.' '.' '.' '.' '.' '#' '.' '.' '.'
 '6' '1' '7' '*' '.' '.' '.' '.' '.' '.'
 '.' '.' '.' '.' '.' '+' '.' '5' '8' '.'
 '.' '.' '5' '9' '2' '.' '.' '.' '.' '.'
 '.' '.' '.' '.' '.' '.' '7' '5' '5' '.'
 '.' '.' '.' '$' '.' '*' '.' '.' '.' '.'
 '.' '6' '6' '4' '.' '5' '9' '8' '.' '.'

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 3, 2023, 8:54am UTC](https://discourse.julialang.org/t/how-to-read-a-textfile-directly-as-matrix-char/107070/3 "2023-12-03T08:54:48Z")

</div>

> [@jar1](#):
>
> `stack(collect.(readlines(b)); dims=1)`

No need to collect:

```julia
stack(readlines(file); dims=1)

```

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [December 3, 2023, 9:44am UTC](https://discourse.julialang.org/t/how-to-read-a-textfile-directly-as-matrix-char/107070/4 "2023-12-03T09:44:20Z")

</div>

I slightly prefer `eachline` over `readline` for AoC problems.

Also sometimes it helps to have something lime the following at the ready in case you need to customize processing.

```julia-repl
julia> using Chain

julia> input = IOBuffer(raw"""
       467..114..
       ...*......
       ..35..633.
       ......#...
       617*......
       .....+.58.
       ..592.....
       ......755.
       ...$.*....
       .664.598..
       """);

julia> @chain input begin
           read
           Char.(_)
           reshape(_, findfirst(==('\n'), _), :)
           _[1:end-1, :]
           permutedims(_, (2,1))
       end
10×10 Matrix{Char}:
 '4' '6' '7' '.' '.' '1' '1' '4' '.' '.'
 '.' '.' '.' '*' '.' '.' '.' '.' '.' '.'
 '.' '.' '3' '5' '.' '.' '6' '3' '3' '.'
 '.' '.' '.' '.' '.' '.' '#' '.' '.' '.'
 '6' '1' '7' '*' '.' '.' '.' '.' '.' '.'
 '.' '.' '.' '.' '.' '+' '.' '5' '8' '.'
 '.' '.' '5' '9' '2' '.' '.' '.' '.' '.'
 '.' '.' '.' '.' '.' '.' '7' '5' '5' '.'
 '.' '.' '.' '$' '.' '*' '.' '.' '.' '.'

```

I actually prefer to leave out the `permutedims`.
