我正在尝试将从数据库中提取的数字数据写入Float64[]
。原始数据以::ASCIIString
格式呈现,因此尝试将其推送到数组会出现以下错误:
julia> push!(a, "1")
ERROR: MethodError: `convert` has no method matching convert(::Type{Float64}, ::ASCIIString)
This may have arisen from a call to the constructor Float64(...),
since type constructors fall back to convert methods.
Closest candidates are:
call{T}(::Type{T}, ::Any)
convert(::Type{Float64}, ::Int8)
convert(::Type{Float64}, ::Int16)
...
in push! at array.jl:432
尝试直接转换数据会出现同样的错误:
julia> convert(Float64, "1")
ERROR: MethodError: `convert` has no method matching convert(::Type{Float64}, ::ASCIIString)
This may have arisen from a call to the constructor Float64(...),
since type constructors fall back to convert methods.
Closest candidates are:
call{T}(::Type{T}, ::Any)
convert(::Type{Float64}, ::Int8)
convert(::Type{Float64}, ::Int16)
...
在我知道数据是数字的情况下,有没有一种方法可以在推送之前进行转换?
附:我正在使用版本0.4.0
tryparse(Float64,x)
代替parse
。它会返回一个可为空的Float类型,如果字符串无法解析,它将为null。 - Dan Getz