# Julia equivalent of Python (numpy.savetxt) and Matlab (save) commands?

**URL:** <https://discourse.julialang.org/t/julia-equivalent-of-python-numpy-savetxt-and-matlab-save-commands/41738>\
**Category:** Data\
**Created:** [June 19, 2020, 5:44pm UTC](https://discourse.julialang.org/t/julia-equivalent-of-python-numpy-savetxt-and-matlab-save-commands/41738 "2020-06-19T17:44:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dynova](https://avatars.discourse-cdn.com/v4/letter/d/e47c2d/32.png) [@dynova](https://discourse.julialang.org/u/dynova)\
**Post date:** [June 19, 2020, 5:44pm UTC](https://discourse.julialang.org/t/julia-equivalent-of-python-numpy-savetxt-and-matlab-save-commands/41738/1 "2020-06-19T17:44:23Z")

</div>

What is the Julia equivalent of the following Python and MATLAB codes?  
I’m looking for identical formatting in the output.

After searching the documentation (e.g. JLD, CSV, DataFrames), I have not found a satisfactory answer, hence I’m asking here.

(MATLAB) Input:

```julia
p = [1 2]
q = [3 4]
save pqfile.txt p q -ascii -double

```

(MATLAB) Output:

```julia
1.0000000000000000e+00 2.0000000000000000e+00
3.0000000000000000e+00 4.0000000000000000e+00

```

(Python) Input:

```julia
import numpy as np
p = np.array([1, 2])
q = np.array([3, 4])
np.savetxt("pqfile.txt", [p, q], fmt='%.16e')

```

(Python) Output:

```julia
1.0000000000000000e+00 2.0000000000000000e+00
3.0000000000000000e+00 4.0000000000000000e+00

```

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [June 19, 2020, 5:50pm UTC](https://discourse.julialang.org/t/julia-equivalent-of-python-numpy-savetxt-and-matlab-save-commands/41738/2 "2020-06-19T17:50:25Z")

</div>

Would CSV work? Check out DelimitedFiles.

---

<div class="post-metadata">

**Author:** ![jipolanco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jipolanco/32/12129_2.png) [@jipolanco](https://discourse.julialang.org/u/jipolanco)\
**Post date:** [June 19, 2020, 6:18pm UTC](https://discourse.julialang.org/t/julia-equivalent-of-python-numpy-savetxt-and-matlab-save-commands/41738/3 "2020-06-19T18:18:15Z")

</div>

The easiest way is to use `writedlm`, but that won’t give you the exact same formatting:

```julia
using DelimitedFiles
p = [1, 2]
q = [3, 4]
writedlm("pqfile.txt", (p, q), " ")

# Output:
# 1 2
# 3 4

```

If you want more control over the formatting you can write the file manually using the `@printf` macro and iterating over the arrays.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [June 19, 2020, 6:23pm UTC](https://discourse.julialang.org/t/julia-equivalent-of-python-numpy-savetxt-and-matlab-save-commands/41738/4 "2020-06-19T18:23:02Z")

</div>

Adding to the above questions, also check [Formatting.jl](https://github.com/JuliaIO/Formatting.jl) if you are used to Python syntax.

---

<div class="post-metadata">

**Author:** ![dynova](https://avatars.discourse-cdn.com/v4/letter/d/e47c2d/32.png) [@dynova](https://discourse.julialang.org/u/dynova)\
**Post date:** [June 19, 2020, 7:30pm UTC](https://discourse.julialang.org/t/julia-equivalent-of-python-numpy-savetxt-and-matlab-save-commands/41738/5 "2020-06-19T19:30:36Z")

</div>

I’m looking into Formatting.jl - does this package give only string output, or can it be used to write to files?

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [June 19, 2020, 8:37pm UTC](https://discourse.julialang.org/t/julia-equivalent-of-python-numpy-savetxt-and-matlab-save-commands/41738/6 "2020-06-19T20:37:39Z")

</div>

You can write any string to a file using  
[https://docs.julialang.org/en/v1/base/io-network/#Base.open](https://docs.julialang.org/en/v1/base/io-network/#Base.open)
