# Writing array to file with format

**URL:** <https://discourse.julialang.org/t/writing-array-to-file-with-format/96175>\
**Category:** New to Julia\
**Tags:** io, formatting\
**Created:** [March 16, 2023, 11:08am UTC](https://discourse.julialang.org/t/writing-array-to-file-with-format/96175 "2023-03-16T11:08:34Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andres\_Legarra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andres_legarra/32/8786_2.png) [@Andres\_Legarra](https://discourse.julialang.org/u/Andres_Legarra)\
**Post date:** [March 16, 2023, 11:08am UTC](https://discourse.julialang.org/t/writing-array-to-file-with-format/96175/1 "2023-03-16T11:08:34Z")

</div>

I’m trying to write an array with predefined format to a file. In Fortran this is :

```julia
do i=size(A,1)
    write(10,'(1000f9.5)')A(i,:)
enddo

```

in Julia this is:

```julia
for row in eachrow(A)
    write(io,row)
end

```

but (contrary to python or fortran) `write` does not directly accept formats (or I couldn’t find how).

I am aware of `Printf` and of @printf and of @sprintf and of this thread [How to repeat the format in @printf?](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695) but I have not been able to find a neat solution or a clear example that would not involve joins, broadcasting functions, etc. Am I missing something?

thanks, Andres

---

<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 16, 2023, 11:33am UTC](https://discourse.julialang.org/t/writing-array-to-file-with-format/96175/2 "2023-03-16T11:33:57Z")

</div>

> [@Andres\_Legarra](#):
>
> I am aware of `Printf` and of @printf and of @sprintf and of this thread [How to repeat the format in @printf?](https://discourse.julialang.org/t/how-to-repeat-the-format-in-printf/66695) but I have not been able to find a neat solution or a clear example that would not involve joins, broadcasting functions, etc. Am I missing something?

What’s wrong with a loop?

```julia
for i = axes(A, 1)
    for j = axes(A, 2)
        @printf(io, "%9.5f", A[i,j])
    end
    println(io)
end

```

> [@Andres\_Legarra](#):
>
> (contrary to python or fortran) `write` does not directly accept formats (or I couldn’t find how).

`write` in Julia is for raw binary output, not for text representations of numeric data.

---

<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 16, 2023, 11:39am UTC](https://discourse.julialang.org/t/writing-array-to-file-with-format/96175/3 "2023-03-16T11:39:13Z")

</div>

That’s definitely one thing where Fortran has a simpler syntax than any other language I know.

In Julia I do this:

```julia
julia> using Printf

julia> fmt(x) = @sprintf("%9.5f",x)
fmt (generic function with 1 method)

julia> x = rand(3);

julia> println(fmt.(x)...)
  0.76353 0.61711 0.72342

```

and yes, it involves broadcasting, splatting, etc. I don’t know of any much cleaner alternative (there are ways to make it _faster_, but usually that’s not important).

(as mentioned above, the `write` function in Fortran is `println` in julia, `write` in Julia is something else).

---

<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 16, 2023, 11:47am UTC](https://discourse.julialang.org/t/writing-array-to-file-with-format/96175/4 "2023-03-16T11:47:25Z")

</div>

> [@lmiq](#):
>
> `println(fmt.(x)...)`

This seems like a prime example of why contorting yourself to avoid writing loops is a bad habit in programming. Not only is allocating 1000 strings and splattting them just to write them terrible for performance, but it’s arguably a lot less clear than just writing a 3-line loop.

Also note that the OP asked for a solution “that would **not** involve joins, broadcasting functions, etc” (emphasis added).

---

<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 16, 2023, 12:07pm UTC](https://discourse.julialang.org/t/writing-array-to-file-with-format/96175/5 "2023-03-16T12:07:25Z")

</div>

I know about the horrible performance limitations of that, but I have to be sincere here, I didn’t find that loop clear (after some thought, now I get it 🙂 ). There are quite a few assumptions there which may be obvious for some.

For instance, that `@printf(io, ...)` will write row-wise, and that `println(io)` is being used to print a single new line character. (It would be clearer for me if you had used `println(io,'\n')`.

What is curious here is that this tentative to “vectorize” the writing is perhaps one unique example where Fortran provides a “vectorized” alternative that, indeed, promotes that kind of bad habit. And in Fortran one gets used to that just because the standard `write` includes the new line character, such that it is less natural to write a double loop than a single loop with a row-vectorized writing in the middle.

By the way, if I wanted to print that on the REPL, what `io` should be? (edit: found it: `io = stdout`)

---

<div class="post-metadata">

**Author:** ![Andres\_Legarra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andres_legarra/32/8786_2.png) [@Andres\_Legarra](https://discourse.julialang.org/u/Andres_Legarra)\
**Post date:** [March 16, 2023, 12:27pm UTC](https://discourse.julialang.org/t/writing-array-to-file-with-format/96175/6 "2023-03-16T12:27:33Z")

</div>

Thank you both @lmiq and @stevengj . Both solutions are natural and elegant. I am not agains loops (I did not mention loops; I did mention broadscats). Anyway all previous solutions that I could see where extremely complicated.

Fortran (like Julia) provides _many_ vectorized options equivalent _e.g._ to `.=` and I was surprised not to find them for printing in Julia.

---

<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 16, 2023, 12:52pm UTC](https://discourse.julialang.org/t/writing-array-to-file-with-format/96175/7 "2023-03-16T12:52:43Z")

</div>

> [@Andres\_Legarra](#):
>
> Fortran (like Julia) provides _many_ vectorized options equivalent _e.g._ to `.=` and I was surprised not to find them for printing in Julia.

Yes, true, but most of the vectorized operations of modern fortran translate quite clearly to equivalent loops. Specifically when writing files the vectorized operation is sort of forced upon one, since a double loop writing one element at a time, row-wise, is very unnatural in Fortran.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [March 16, 2023, 12:53pm UTC](https://discourse.julialang.org/t/writing-array-to-file-with-format/96175/8 "2023-03-16T12:53:44Z")

</div>

You could also use something like [PrettyTables.jl](https://ronisbr.github.io/PrettyTables.jl/stable/) which has lots of utilities for this. It is mostly for display, but you can output to file also.

---

<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 16, 2023, 1:55pm UTC](https://discourse.julialang.org/t/writing-array-to-file-with-format/96175/9 "2023-03-16T13:55:02Z")

</div>

> [@lmiq](#):
>
> It would be clearer for me if you had used `println(io,'\n')`

That writes two newlines. `println(io)` is equivalent to `print(io, '\n')`.

> [@lmiq](#):
>
> By the way, if I wanted to print that on the REPL, what `io` should be? (edit: found it: `io = stdout`)

If you omit the `io` argument entirely, it defaults to `stdout`.

---

<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 16, 2023, 1:58pm UTC](https://discourse.julialang.org/t/writing-array-to-file-with-format/96175/10 "2023-03-16T13:58:41Z")

</div>

> [@Andres\_Legarra](#):
>
> Fortran (like Julia) provides _many_ vectorized options equivalent _e.g._ to `.=` and I was surprised not to find them for printing in Julia.

There are lots of vectorized array I/O routines in Julia, but they are mostly for read/writing standard formats (CSV, HDF5, raw binary, …). Fortran 77 style fixed-width ASCII formats, which date back to the days of [card readers](https://en.wikipedia.org/wiki/Computer_programming_in_the_punched_card_era), are much less common these days for storage, and there are better options if you simply want pretty printing.

(That being said, there is still some interest in fixed-width formats, e.g. there is an open issue to [add this to CSV.jl](https://github.com/JuliaData/CSV.jl/issues/601). I’m guessing that eventually there will be optimized library support, but it doesn’t seem like something that is a burning issue for many developers.)

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [March 16, 2023, 6:47pm UTC](https://discourse.julialang.org/t/writing-array-to-file-with-format/96175/11 "2023-03-16T18:47:57Z")

</div>

> [@stevengj](#):
>
> there is still some interest in fixed-width formats

Fixed-width tables appear pretty often in the wild IME, see my [Alexander Plavin / FixedWidthTables.jl · GitLab](https://gitlab.com/aplavin/FixedWidthTables.jl) package for convenient and general reading of them. Potentially, writing could be in scope as well, it’s just that I personally never needed to do that.

---

<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:** [March 16, 2023, 7:45pm UTC](https://discourse.julialang.org/t/writing-array-to-file-with-format/96175/12 "2023-03-16T19:45:29Z")

</div>

Same stuff but using `foreach`:

```julia
using Printf
A = rand(100,10)

io = IOBuffer()
for r in eachrow(A)
    foreach(x -> @printf(io, "%9.5f", x), r)
    println(io)
end
println(String(take!(io)))

```
