liveBook · Manning

Parsing

As explained in Julia as a Second Language

Parsing is the process of interpreting a textual representation to produce a value of a specified type.

Input from a file or keyboard arrives as text, even when it looks like a number. Parsing interprets that text as a value of a chosen type. In Julia, put the target type first: `parse(Int, "42")` returns `42`, while `parse(Float64, "42")` returns `42.0`. The same text can therefore produce different values; `parse(Bool, "true")` and `parse(Bool, "1")` both return `true`. section 5.3.2

Turn a sales field into a price

Suppose a line read from a pizza-sales file is `"hawaiian,S,10.5"`. Try `fields = split(line, ',')` after assigning that line to `line`. Then `fields[3]` is text, so multiplying it by `1.25` does not work. Use `price = parse(Float64, fields[3])`; now `price * 1.25` produces `13.125`. Splitting finds the field; parsing makes it usable in arithmetic. section 5.3.2

Listing 5.8 Loading pizza sales data
function load_pizzatable(io)
    pizzas = []                               #1
    readline(io)                              #2
    while !eof(io)                            #3
      pz = split(readline(io), ',')
      pr = parse(Float64, price(pz))          #4
      sz = portion(pz)
      push!(pizzas, (name(pz), sz[1], pr))    #5
    end
    pizzas
end

The loading function puts these steps together: skip the header, read each line until end-of-file, split it, parse the price as `Float64`, and collect a pizza tuple. section 5.3.2

For numbers, the target type can be more specific: `parse(UInt8, "42")`, `parse(Int16, "42")`, and `parse(Float64, "0.42")` request different numeric types. Integer parsing assumes base 10 unless you specify another base: `parse(Int, "101")` is `101`, but `parse(Int, "101", base=2)` is `5`. To go the other direction, `string(5, base=2)` returns `"101"`. section 11.2.2 section 11.2.2

This approach is useful for small, known input layouts. A line-by-line pizza-sales loader demonstrates it; parsing a CSV file of rocket-engine data is a further input/output application. section 5.3.2 section 17.0

How Julia as a Second Language covers it

How deep it goes
Works through typed conversions and a line-by-line CSV loader, then adds numeric bases; defers more elaborate CSV-reading approaches.
Examples
Uses pizza-sales rows, Boolean strings, numeric type choices, and the different interpretations of `"101"` across bases.
What it assumes
Introduces text-to-value conversion through short function calls and explains why fields read as strings cannot be used directly as numbers.
What you can do after
Apply it via Julia’s `parse` to turn selected input fields into numeric or Boolean values and choose a numeric base when needed.
How it connects
Links parsing to strings, numeric types, `readline`, `split`, CSV input, and conversion back to strings.

Also explained in

Compare how 13 books explain parsing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Julia as a Second Language. The section links point to the parts of the book it draws on.