# Creating Fixed Width Output Files

**URL:** <https://discourse.julialang.org/t/creating-fixed-width-output-files/98260>\
**Category:** General Usage\
**Created:** [May 3, 2023, 3:06pm UTC](https://discourse.julialang.org/t/creating-fixed-width-output-files/98260 "2023-05-03T15:06:14Z")\
**Posts on this page:** 3\
**Page:** 1

<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:06pm UTC](https://discourse.julialang.org/t/creating-fixed-width-output-files/98260/1 "2023-05-03T15:06:14Z")

</div>

I am unsatisfied with the discussion [here](https://discourse.julialang.org/t/write-fixed-width-table/23689), so am opening a new thread to find a solution to this.

In Python, it is quite simple to save a data frame or dictionary with fixed width columns as a `.dat` file. This usability does not seem to exist in Julia. What is the simplest method possibly (ideally avoiding some obscure package) to construct variable size delimiters for saving human readable .dat files?

To give a minimal working example of most of what I am hoping for

```julia

using DelimitedFiles, DataFrames

inputfile = "/home/local_directory_example/inputfile.json"
outputfile = "/home/local_directory_example/output_directory/"
# some dictionary that will get created
d = Dict("ALPHA TEST" => [5.240,1.2340,23.445625633676,4.30,5.2340], "B" => [4.235,3.0,2.245,5.245,1.245])
df = DataFrame(d)
fobj = open(inputfile ,"r")
input_file_contents = read(fobj, String)
fobj2=open(filename*"_test.dat", "w")
write(fobj2, input_file_contents)
writedlm(fobj2,Iterators.flatten(([names(df)], eachrow(df))))
close(fobj)
close(fobj2)

```

This creates a header in my .dat file with my input file and saves the output data but the formatting is unsightly.

---

<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, 2023, 4:00pm UTC](https://discourse.julialang.org/t/creating-fixed-width-output-files/98260/2 "2023-05-03T16:00:42Z")

</div>

That’s neither a minimal working example nor is it running (`inputfile.json` is missing). A good MWE can be copy-pasted into a fresh Julia session and ran without errors (unless the error is part of the example 😉 ).

I am wondering how your “quite simple” Python implementation looks like, I am pretty sure you can translate it more or less one-to-one to Julia.

Anyways, maybe this is what you are looking for (no “obscure” packages, whatever that means):

```julia
using Printf
using DataFrames

n = 10

df = DataFrame(
    Dict("A" => rand(n), "B" => rand(n))
)

entry_fmt = "%9.3f"

open("out.dat", "w") do fobj
    row_fmt = Printf.Format(entry_fmt^ncol(df) * "\n")
    for row ∈ eachrow(df)
        Printf.format(fobj, row_fmt, values(row)...)
    end
end

```

```shell
$ cat out.dat
    0.944 0.390
    0.811 0.148
    0.542 0.832
    0.874 0.840
    0.026 0.467
    0.611 0.962
    0.745 0.851
    0.358 0.604
    0.179 0.921
    0.298 0.062

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [May 3, 2023, 5:43pm UTC](https://discourse.julialang.org/t/creating-fixed-width-output-files/98260/3 "2023-05-03T17:43:27Z")

</div>

[perhpas this function](https://docs.juliahub.com/InMemoryDatasets/cS87e/0.7.15/man/formats/) of InMemorydataset pkg, it suits you
