# How to create a file where the columns are separated by equal space

**URL:** <https://discourse.julialang.org/t/how-to-create-a-file-where-the-columns-are-separated-by-equal-space/46240>\
**Category:** General Usage\
**Created:** [September 8, 2020, 5:28am UTC](https://discourse.julialang.org/t/how-to-create-a-file-where-the-columns-are-separated-by-equal-space/46240 "2020-09-08T05:28:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nova](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Nova](https://discourse.julialang.org/u/Nova)\
**Post date:** [September 8, 2020, 5:28am UTC](https://discourse.julialang.org/t/how-to-create-a-file-where-the-columns-are-separated-by-equal-space/46240/1 "2020-09-08T05:28:21Z")

</div>

Here is the code I use to create the file:

```julia
ar = [[1, 85645, 197111, 197240], [1, 203753],[1, 24]]                  

loop = 0
for i =1:3#length(ar)
    val = ar[i]' .-1
    print(ff, "NetDegree : ", length(val), " ", "n", loop)
    print(ff, '\n')
    for jj = 1:length(val)
        vv = val[jj]
        print(ff, "\t", "o", val[jj], " I : ", 5.000000, " ", -5.000000)
        print(ff, "\n")
    end #end of jj

    loop +=1

end # end of for i

end

```

The output is:

![Screen Shot 2020-09-08 at 1.23.35 AM](https://global.discourse-cdn.com/julialang/original/3X/4/b/4bcc163dc93e81ee5c6c7fe9c45e3e227c2e00cb.png)

but I need it to be like the following:  
 ![Screen Shot 2020-09-08 at 1.25.20 AM](https://global.discourse-cdn.com/julialang/original/3X/1/7/17938d19254fb98fdf3700cce8e28ea19eb0245b.png)

Please ignore the mismatch between the numbers. My question is how to make each line with the same format.

---

<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:** [September 8, 2020, 7:05am UTC](https://discourse.julialang.org/t/how-to-create-a-file-where-the-columns-are-separated-by-equal-space/46240/2 "2020-09-08T07:05:33Z")

</div>

If you know the maximum width, you can pad using `@printf`, see

[https://docs.julialang.org/en/v1/stdlib/Printf/](https://docs.julialang.org/en/v1/stdlib/Printf/)

> **[printf | Width field](https://en.wikipedia.org/wiki/Printf_format_string#Width_field)**
>
> The Width field specifies a minimum number of characters to output and is typically used to pad fixed-width fields in tabulated output, where the fields would otherwise be smaller, although it does not cause truncation of oversized fields.
> The width field may be omitted, or a numeric integer value, or a dynamic value when passed as another argument when indicated by an asterisk \*. For example, printf("%\*d", 5, 10) will result in 10 being printed, with a total width of 5 characters.
> Though no...

Otherwise, you can convert the fields with `string` in one pass and get the maximum width, then print padding to that in a second pass. See eg `lpad`.

---

<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:** [September 8, 2020, 7:06am UTC](https://discourse.julialang.org/t/how-to-create-a-file-where-the-columns-are-separated-by-equal-space/46240/3 "2020-09-08T07:06:47Z")

</div>

(also, moving this to _Usage_ from _Performance_)

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [September 8, 2020, 3:16pm UTC](https://discourse.julialang.org/t/how-to-create-a-file-where-the-columns-are-separated-by-equal-space/46240/5 "2020-09-08T15:16:11Z")

</div>

Yes, as @Tamas_Papp said, `@printf` is quite easy to use for this:

```julia
using Printf
ar = [[1, 85645, 197111, 197240], [1, 203753],[1, 24]]                  

loop = 0
for i =1:length(ar)
    val = ar[i]' .-1
    @printf("NetDegree : %-2d n%d ", length(val), loop)
    @printf("\n")
    for jj = 1:length(val)
        vv = val[jj]
        @printf("\t\t o%-6d I : %9.6f, %9.6f", val[jj], 5.000000, -5.000000)
        @printf("\n")
    end # end of jj
    loop += 1
end # end of i

```

Output:

```julia
NetDegree : 4 n0 
         o0 I : 5.000000, -5.000000
         o85644 I : 5.000000, -5.000000
         o197110 I : 5.000000, -5.000000
         o197239 I : 5.000000, -5.000000
NetDegree : 2 n1 
         o0 I : 5.000000, -5.000000
         o203752 I : 5.000000, -5.000000
NetDegree : 2 n2 
         o0 I : 5.000000, -5.000000
         o23 I : 5.000000, -5.000000

```

---

<div class="post-metadata">

**Author:** ![Nova](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Nova](https://discourse.julialang.org/u/Nova)\
**Post date:** [September 9, 2020, 1:48pm UTC](https://discourse.julialang.org/t/how-to-create-a-file-where-the-columns-are-separated-by-equal-space/46240/6 "2020-09-09T13:48:11Z")

</div>

Can you also help me to write this into a file. I tried this:

```julia
ff = open("test.txt", "w")

ss = Array{String, 0}

ss = @sprintf("\t o%-6d I : %9.6f, %9.6f", val[jj], 5.000000, -5.000000)

print(ff, ss)

```

This works but it doesn’t generate the tab space at the beginning and writes “\t” instead. I don’t know why using @printf instead of @sprintf doesn’t work at all.
