# Saving Primes Number in a range to CSV

**URL:** https://discourse.julialang.org/t/saving-primes-number-in-a-range-to-csv/95480
**Category:** General Usage
**Tags:** package
**Created:** [March 3, 2023, 4:46am UTC](https://discourse.julialang.org/t/saving-primes-number-in-a-range-to-csv/95480 "2023-03-03T04:46:05Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)
#### Post date: [March 3, 2023, 4:46am UTC](https://discourse.julialang.org/t/saving-primes-number-in-a-range-to-csv/95480/1 "2023-03-03T04:46:05Z")

</div>

Hi all,

I want to save all prime numbers in certain range into CSV. But why it does not saved?

I am following this forum:

> <https://stackoverflow.com/questions/63234061/how-to-save-the-outputs-of-a-code-in-csv-files-in-julia>

```julia
using Primes, CSV

CSV.write("./out.csv", (p = primes(1,1000000)))

```

---

<div class="post-metadata">

### Author: ![chiion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chiion/32/37002_2.png) [@chiion](https://discourse.julialang.org/u/chiion)
#### Post date: [March 3, 2023, 6:54am UTC](https://discourse.julialang.org/t/saving-primes-number-in-a-range-to-csv/95480/2 "2023-03-03T06:54:00Z")

</div>

`CSV.write` only accept Tables.jl interface input (see [doc](https://csv.juliadata.org/stable/writing.html)).

So it can be fixed by convert the prime vector to a table (alternatively, `DataFrames` also works):

```julia
using Primes, CSV, Tables
CSV.write("./out.csv", Tables.table(primes(1,1000000)))

```

---

<div class="post-metadata">

### Author: ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)
#### Post date: [March 3, 2023, 8:30am UTC](https://discourse.julialang.org/t/saving-primes-number-in-a-range-to-csv/95480/3 "2023-03-03T08:30:53Z")

</div>

It works great. Thanks

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [March 3, 2023, 9:30am UTC](https://discourse.julialang.org/t/saving-primes-number-in-a-range-to-csv/95480/4 "2023-03-03T09:30:36Z")

</div>

That’s **a** solution, but I don’t think it should be **the** solution.

It’s correct that `CSV.write` requires a `Tables` compatible object, but that doesn’t mean you actually need `Tables` - a `NamedTuple` is a valid table and built into base.

Your problem in translating my Stackoverflow answer to your case is that to construct a one element tuple you need to append a comma before the closing parentheses, consider:

```julia
julia> (x = rand(5))
5-element Vector{Float64}:
 0.6238084279345859
 0.8932712676662397
 0.36832615201955377
 0.9091819412601923
 0.5210877983924257

julia> (x = rand(5),)
(x = [0.04347875708971949, 0.1589617604772473, 0.8344142933023203, 0.632009784788371, 0.5841220686173417],)

```

in the first case, the brackets don’t actually do anything, while in the second case they construct a `NamedTuple`. So you should do `CSV.write("./out.csv", (p = primes(10_000), ))` and things will work out without any extra dependencies.

The reason you need the extra comma is that we don’t want simple expressions wrapped in brackets to produce tuples, consider:

```julia
julia> (5 - 3)*6
12

julia> (5 - 3,)*6
ERROR: MethodError: no method matching *(::Tuple{Int64}, ::Int64)

```

---

<div class="post-metadata">

### Author: ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)
#### Post date: [March 3, 2023, 10:55am UTC](https://discourse.julialang.org/t/saving-primes-number-in-a-range-to-csv/95480/5 "2023-03-03T10:55:13Z")

</div>

> [@nilshg](#):
>
> `CSV.write("./out.csv", (p = primes(10_000), ))`

Yes, it works too without extra dependency… I thought I do not need `,`
