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

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




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.

Perhaps looking at could be beneficial, but for you explicit question:

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

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

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?


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.

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.

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

1 Like

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

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> using DelimitedFiles

julia> f = readdlm("new.txt")
3×1 Matrix{Any}:

Saving the file without BOM it works:

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

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

1 Like


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

1 Like