# Write fixed-width table

**URL:** https://discourse.julialang.org/t/write-fixed-width-table/23689
**Category:** General Usage
**Created:** [April 30, 2019, 8:05am UTC](https://discourse.julialang.org/t/write-fixed-width-table/23689 "2019-04-30T08:05:06Z")
**Posts on this page:** 8
**Page:** 1

<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: [April 30, 2019, 8:05am UTC](https://discourse.julialang.org/t/write-fixed-width-table/23689/1 "2019-04-30T08:05:06Z")

</div>

Hi! I cannot find a simple way to write a dataframe/array of tuples/… to file with fixed-width columns. `CSV` package does not support fixed width or formatting in general; manually looping over rows and calling `@sprintf` makes it really verbose and error-prone to handle `missing`s - not even talking about manually changing the code each time a column is added/removed.

Am I missing something here?

---

<div class="post-metadata">

### Author: ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)
#### Post date: [April 30, 2019, 8:24am UTC](https://discourse.julialang.org/t/write-fixed-width-table/23689/2 "2019-04-30T08:24:02Z")

</div>

Have you tried this package? [https://github.com/ronisbr/PrettyTables.jl](https://github.com/ronisbr/PrettyTables.jl)

---

<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: [May 2, 2019, 1:46pm UTC](https://discourse.julialang.org/t/write-fixed-width-table/23689/3 "2019-05-02T13:46:16Z")

</div>

Thanks for suggestion, it mostly works for simple scenarios!  
However, anything even slightly more complicated requires much more code, and becomes more and more verbose with repetitions:

```julia
data = [[rand() < 0.8 ? (col == "C" ? "Abc" : rand()) : missing for a = 0:10] for col = ["A", "B", "C"]];
df = DataFrame(data)

my_format(x::Float64, _) = @sprintf("%.2f", x)
my_format(x::String, _) = x
my_format(::Missing, _) = ""

pretty_table(df, borderless; formatter=Dict(0 => my_format, 2 => (x, _) -> ismissing(x) ? "" : @sprintf("%.5f", x)))

```

---

<div class="post-metadata">

### Author: ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)
#### Post date: [May 3, 2019, 3:22pm UTC](https://discourse.julialang.org/t/write-fixed-width-table/23689/4 "2019-05-03T15:22:06Z")

</div>

Repetitions are a sign that you might write your own macros 😉

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [May 3, 2019, 3:30pm UTC](https://discourse.julialang.org/t/write-fixed-width-table/23689/5 "2019-05-03T15:30:15Z")

</div>

> [@aplavin](#):
>
> becomes more and more verbose with repetitions

Can you clarify where? I find the above code quite compact and readable. You can always introduce a function that wraps a function to ignore the second argument to get rid of the `_`s, but here the gain is minor.

---

<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: [May 3, 2019, 3:37pm UTC](https://discourse.julialang.org/t/write-fixed-width-table/23689/6 "2019-05-03T15:37:53Z")

</div>

Compare

```julia
my_format(x::Float64, _) = @sprintf("%.2f", x)
my_format(x::String, _) = x
my_format(::Missing, _) = ""

pretty_table(df, borderless; formatter=Dict(
    0 => my_format,
    2 => (x, _) -> ismissing(x) ? "" : @sprintf("%.5f", x),
    3 => (x, _) -> ismissing(x) ? "" : @sprintf("%.4f", x),
    7 => (x, _) -> ismissing(x) ? "" : @sprintf("%.3f", x)))

```

to this (hypothetical) example:

```julia
pretty_table(df, borderless; format=Dict(
    Missing => "", Real => "%.2f", String => "%s",
    :x2 => "%.5f", :x3 => "%.4f", :x7 => "%.4f"))

```

or something along these lines.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [May 3, 2019, 3:50pm UTC](https://discourse.julialang.org/t/write-fixed-width-table/23689/7 "2019-05-03T15:50:23Z")

</div>

There are two (orthogonal) improvements that could be made here:

1. As @tamasgal suggested, define a macro that transforms `@myformatter "%2f"` to `(x, _) -> ismissing(x) ? "" : @sprintf("%.5f", x)` etc. This is quite easy to do. If you use something like `Formatting.sprintf1`, it does not even have to be a macro.

2. Open an issue in PrettyTables, asking for lookup by column.

---

<div class="post-metadata">

### Author: ![andrewkhardy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrewkhardy/32/27311_2.png) [@andrewkhardy](https://discourse.julialang.org/u/andrewkhardy)
#### Post date: [May 3, 2023, 3:09pm UTC](https://discourse.julialang.org/t/write-fixed-width-table/23689/8 "2023-05-03T15:09:29Z")

</div>

@tamasgal @Tamas_Papp

The discussion here did not answer my question, so I’ve made a new thread [here](https://discourse.julialang.org/t/creating-fixed-width-output-files/98260), but if there’s a simpler solution you can provide, I would be happy to close it.
