# Reading values from .txt file

**URL:** <https://discourse.julialang.org/t/reading-values-from-txt-file/89466>\
**Category:** New to Julia\
**Tags:** io\
**Created:** [October 28, 2022, 8:37pm UTC](https://discourse.julialang.org/t/reading-values-from-txt-file/89466 "2022-10-28T20:37:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bdas123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bdas123/32/32142_2.png) [@bdas123](https://discourse.julialang.org/u/bdas123)\
**Post date:** [October 28, 2022, 8:37pm UTC](https://discourse.julialang.org/t/reading-values-from-txt-file/89466/1 "2022-10-28T20:37:11Z")

</div>

Hello, I am exporting certain values from Python into a txt file using the following command:

```julia
a = np.random.rand(2)

np.savetxt('a.txt',a)

```

How can I call this .txt file from Julia and store the value into a variable?

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [October 28, 2022, 9:10pm UTC](https://discourse.julialang.org/t/reading-values-from-txt-file/89466/2 "2022-10-28T21:10:33Z")

</div>

For this simple case (a single column vector)

```julia
julia> a = open("a.txt") do f
           readlines(f) |> (s->parse.(Float64, s))
       end
2-element Vector{Float64}:
 0.9445125367265572
 0.10241256516487685

```

For more complicated cases, the [`DelimitedFiles`](https://docs.julialang.org/en/v1/stdlib/DelimitedFiles/) standard library is a good start.

---

<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:** [October 28, 2022, 9:30pm UTC](https://discourse.julialang.org/t/reading-values-from-txt-file/89466/3 "2022-10-28T21:30:28Z")

</div>

May also consider the one-liner:

```julia
a = parse.(Float64, readlines("a.txt"))

```

But the simpler is:

```julia
using DelimitedFiles
a = readdlm("a.txt")

```

---

<div class="post-metadata">

**Author:** ![bdas123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bdas123/32/32142_2.png) [@bdas123](https://discourse.julialang.org/u/bdas123)\
**Post date:** [October 31, 2022, 3:33pm UTC](https://discourse.julialang.org/t/reading-values-from-txt-file/89466/4 "2022-10-31T15:33:13Z")

</div>

> [@contradict](#):
>
> ```julia
> |> (s->parse.(Float64, s))
> 
> ```

Awesome, thank you!

In that same thread, I have another question.

I have a list b that contains the following values:

`b = [(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5)]`

I save it to a text file with the following commands

```julia
with open('b.txt', 'w') as value:
    for item in b:
        value.write(f"{item}\n")

```

How do I read a list from a text file in Julia?

I tried the previous commands stated in this thread, and they do not work.

---

<div class="post-metadata">

**Author:** ![bdas123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bdas123/32/32142_2.png) [@bdas123](https://discourse.julialang.org/u/bdas123)\
**Post date:** [October 31, 2022, 3:46pm UTC](https://discourse.julialang.org/t/reading-values-from-txt-file/89466/5 "2022-10-31T15:46:04Z")

</div>

When I execute the following command in Julia:

```julia
b = open("b.txt") do f
            readlines(f)
       end

```

I get the following result:

```julia
"(0, 0)"
 "(0, 1)"
 "(0, 2)"
 "(0, 3)"
 "(0, 4)"
 "(0, 5)"

```

How do I get the result without the quotation marks?

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [October 31, 2022, 4:06pm UTC](https://discourse.julialang.org/t/reading-values-from-txt-file/89466/6 "2022-10-31T16:06:21Z")

</div>

Any particular reason for using a txt file? You could skip manual parsing entirely by using [GitHub - fhs/NPZ.jl: A Julia package that provides support for reading and writing Numpy .npy and .npz files](https://github.com/fhs/NPZ.jl) or similar.

---

<div class="post-metadata">

**Author:** ![bdas123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bdas123/32/32142_2.png) [@bdas123](https://discourse.julialang.org/u/bdas123)\
**Post date:** [October 31, 2022, 7:28pm UTC](https://discourse.julialang.org/t/reading-values-from-txt-file/89466/7 "2022-10-31T19:28:28Z")

</div>

It looks like I learn something new every day. Thank you.

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [October 31, 2022, 11:02pm UTC](https://discourse.julialang.org/t/reading-values-from-txt-file/89466/8 "2022-10-31T23:02:50Z")

</div>

> [@bdas123](#):
>
> How do I get the result without the quotation marks?

I agree that using NPZ.jl is a better solution, but to answer the question as asked:

```julia
function readtuples(filename)
    tvec = Tuple{Int, Int}[]
    for line in eachline(filename)
        words = split(line, ('(', ',', ')'), keepempty=false)
        push!(tvec, tuple(parse.(Int, words)...))
    end
    return tvec
end

```

which can be used as follows:

```julia
julia> readtuples("data.txt")
6-element Vector{Tuple{Int64, Int64}}:
 (0, 0)
 (0, 1)
 (0, 2)
 (0, 3)
 (0, 4)
 (0, 5)

```

---

<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:** [October 31, 2022, 11:40pm UTC](https://discourse.julialang.org/t/reading-values-from-txt-file/89466/9 "2022-10-31T23:40:13Z")

</div>

Also, a regex one-liner:

```julia
[Tuple(parse.(Int, match(r"\((.*),(.*)\)", l).captures)) for l in eachline(file)]

```

where:  
`file = "b.txt"`
