Help with string mapping from input

You could define something crude as

parsefield(T::Type{<:Real}, str::AbstractString) = parse(T, str)
parsefield(::Type{T}, str::T) where T <: AbstractString = str
parsefield(::Type{T}, str::AbstractString) where T <: AbstractString = T(str)

schemas = Dict(["ABC" => (a = Int64, b = Int64, c = Int64, animal = String, put = String)])

function parsewithschema(row, schemas)
    key, fields = split(row, '=')
    schema = schemas[key]
    NamedTuple{keys(schema)}(map(parsefield, schemas[key], split(fields, ',')))
end

then

julia> parsewithschema(inputtext[1], schemas)
(a = 1, b = 2, c = 3, animal = "cats", put = "dogs")

Would need a bit of refinement, whitespace stripping, and validation for real-life applications, but you get the idea. You can then interface into Tables.jl.

Alternatively, leverage the row-reading functionality from one of the CSV reading libraries.

EDIT: Try using Parsers.jl for the fields, as described here.

1 Like