# Convert Any\["0.30", "0.34"\] to \[0.30, 0.34\]

**URL:** <https://discourse.julialang.org/t/convert-any-0-30-0-34-to-0-30-0-34/67374>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [August 30, 2021, 4:59pm UTC](https://discourse.julialang.org/t/convert-any-0-30-0-34-to-0-30-0-34/67374 "2021-08-30T16:59:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nenndul](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nenndul/32/22149_2.png) [@Nenndul](https://discourse.julialang.org/u/Nenndul)\
**Post date:** [August 30, 2021, 4:59pm UTC](https://discourse.julialang.org/t/convert-any-0-30-0-34-to-0-30-0-34/67374/1 "2021-08-30T16:59:13Z")

</div>

I have a little program working and am trying to get the data read in from a file. After poking around a little I found the following method:

using Queryverse

open(“data.csv”) do f

```
# line_number

line = 0

# read till end of file

while ! eof(f)

  # read a new / next line for every iteration

  s = readline(f)

  line += 1

  push!(dataRead,"$s")

end

```

end

This works to create a Array that outputs:

Any[“0.30”, “0.34”]

However, for the rest of my program I need this to just be floats, so it should just read:  
[0.30, 0.34]

I have tried variations on a number of methods proposed on this board, but can’t seem to get it to work. Ideally I guess it would be better if the data was just directly imported as a Float64, but I can’t figure out how to get it to do that.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [August 30, 2021, 5:02pm UTC](https://discourse.julialang.org/t/convert-any-0-30-0-34-to-0-30-0-34/67374/2 "2021-08-30T17:02:27Z")

</div>

Perhaps looking at [https://csv.juliadata.org/](https://csv.juliadata.org/) could be beneficial, but for you explicit question:

```julia
julia> a=Any["0.30", "0.34"]
2-element Vector{Any}:
 "0.30"
 "0.34"

julia> f=parse.(Float64,a)
2-element Vector{Float64}:
 0.3
 0.34

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 30, 2021, 5:10pm UTC](https://discourse.julialang.org/t/convert-any-0-30-0-34-to-0-30-0-34/67374/3 "2021-08-30T17:10:58Z")

</div>

You should really try to avoid arrays of `Any` if you can, they are very bad for performance. Can’t you make an array of strings instead? Or better yet, directly parse each line to float, and push them into a `Float64` array?

---

<div class="post-metadata">

**Author:** ![Nenndul](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nenndul/32/22149_2.png) [@Nenndul](https://discourse.julialang.org/u/Nenndul)\
**Post date:** [August 30, 2021, 5:14pm UTC](https://discourse.julialang.org/t/convert-any-0-30-0-34-to-0-30-0-34/67374/4 "2021-08-30T17:14:36Z")

</div>

Thank you! Yes, that works perfectly for most important part. I tried something like that but must have messed up the syntax.

I am going to keep the question open for a bit for reading directly to float part if that’s okay.

---

<div class="post-metadata">

**Author:** ![Nenndul](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nenndul/32/22149_2.png) [@Nenndul](https://discourse.julialang.org/u/Nenndul)\
**Post date:** [August 30, 2021, 5:16pm UTC](https://discourse.julialang.org/t/convert-any-0-30-0-34-to-0-30-0-34/67374/5 "2021-08-30T17:16:56Z")

</div>

Yes, that would be ideal. I tried to do this with the CSV package, but no luck so far.

I’m guessing it is the $s bit in push!(dataRead,“$s”), but my attempts to change the behavior have not been successful.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 30, 2021, 5:31pm UTC](https://discourse.julialang.org/t/convert-any-0-30-0-34-to-0-30-0-34/67374/6 "2021-08-30T17:31:06Z")

</div>

You don’t need any package. Just `parse` for each line and `push!` to a float array instead of an `Any` array.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [August 30, 2021, 5:47pm UTC](https://discourse.julialang.org/t/convert-any-0-30-0-34-to-0-30-0-34/67374/7 "2021-08-30T17:47:06Z")

</div>

I think the easiest is to use CSV.jl with DataFrames.jl:

```julia
julia> using CSV, DataFrames

julia> csv=DataFrame(CSV.File("new.txt"; header=false))
3×1 DataFrame
 Row │ Column1
     │ Float64
─────┼─────────
   1 │ 1.0
   2 │ 2.0
   3 │ 3.0

```

Or DelimitedFiles.jl but I got directly a problem with my trivial file new.txt which happens to be UTF-8-BOM encoded:

```julia
julia> using DelimitedFiles

julia> f = readdlm("new.txt")
3×1 Matrix{Any}:
  "\ufeff1.0"
 2.0
 3.0

```

Saving the file without BOM it works:

```julia
julia> f = readdlm("new.txt")
3×1 Matrix{Float64}:
 1.0
 2.0
 3.0

julia> vf=f[:]
3-element Vector{Float64}:
 1.0
 2.0
 3.0

```

---

<div class="post-metadata">

**Author:** ![Nenndul](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nenndul/32/22149_2.png) [@Nenndul](https://discourse.julialang.org/u/Nenndul)\
**Post date:** [August 30, 2021, 6:11pm UTC](https://discourse.julialang.org/t/convert-any-0-30-0-34-to-0-30-0-34/67374/8 "2021-08-30T18:11:36Z")

</div>

Perfect!

Thanks, that last method works exactly the way I had in mind.
