CSV reading in mutable struct error reading Int as string

Hi All,
While putting csv data in a struct getting the error for the below.

localpath = string("D:\\crane_intensity\\")
mutable struct GeneralDetail
   B::UInt8
   C::UInt8
   P::UInt8
   T::UInt8
   Cs_m::Array{UInt8,1}
   Cs_mv::Array{UInt8,1}
end

GeneralDetails=GeneralDetail[];

df = CSV.read(string(localpath,"input.csv"))

for row in 1:1
         println(df[row,1],",",df[row,2],",", df[row,3],",",df[row,4],",", df[row,5],",", df[row,6])
     push!(GeneralDetails, GeneralDetail( df[row,1], df[row,2], df[row,3], df[row,4], df[row,5], df[row,6]));

end

RROR: LoadError: MethodError: Cannot convert an object of type String to an object of type UInt8
Closest candidates are:
convert(::Type{T<:Number}, ::T<:Number) where T<:Number at number.jl:6
convert(::Type{T<:Number}, ::Number) where T<:Number at number.jl:7
convert(::Type{T<:Integer}, ::Ptr) where T<:Integer at pointer.jl:23

Stacktrace:
[1] GeneralDetail(::String, ::String, ::String, ::String, ::String, ::String) at D:\crane_intensity\test.jl:4
[2] top-level scope at D:\crane_intensity\test.jl:18
in expression starting at D:\crane_intensity\test.jl:16

Hi,

without the input file, we can’t help you to debug this. Seems like CSV.jl detects some column that you think of as a UInt8 as a string. Can you share (at least some part of) the file?

Bays Cranes PODS Container_Type_Load Crane_speed_moves/min Crane_speed_container/move
18 3 3 5 1, 1, 1 1, 1, 1
4, 6, 8, 10
3, 7, 5
2, 1

It looks like your file has entries like 4, 6, 8, 10 in some cells, which CSV will treat as a String.

CSV is not a great format for saving nested structures like vectors of vectors, but at the same time I wonder what you would expect to happen here when you get to row 2 in your example and the constructor gets called with 4,6,8,10 as the B argument?

1 Like