Example for an AbstractToken from TextParse for a JuliaDB

In JuliaDB’s documentation for loadtable, there is an explanation of how to use an AbstractToken object from TextParse in order to parse columns from a CSV file.

Has anyone managed to do that? Say correctly parse numbers to Nanosecond or some such? Do you have a simple example of that? I understand that I need to implement tryparsenext but I’d appreciate seeing a short example of how to do that for something simple (like the Nanosecond thing).

Thanks!

I’d also love to find the answer to this!

1 Like

The docs in TextParse.jl for this are incorrect right now, a fix is coming.

Here is an example that works:

myparser = CustomParser(Bool) do str, i, len, opts
    if i+3 <= len && str[i:i+3] == "true"
        return Nullable(true), i+4
    elseif i+4 <= len && str[i:i+4] == "false"
        return Nullable(false), i+5
    else
        return Nullable{Bool}(), i
    end
end

csvread("test.csv", colparsers=Dict(2=>myparser))

For the kind of use-case you mentioned, you would probably call one of the existing tryparsenext methods from TextParse first, and then just convert the result into something else.

We should make that use case a bit easier, see here.

3 Likes