Parsing Text into Records in Stages

A staged reader can separate decoding, record boundaries, field conversion, and domain validation so each failure has a location.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace a two-column measurement
  2. Retain source information
  3. Trace one record through decode, split and validation
  4. Test one failure attributable to each phase

Trace a two-column measurement

Read the bytes, decode text, parse station and reading fields, then convert the reading to a number. A row with a valid numeric spelling of minus one can still violate the measurement rule. Structural versus domain validation explains that final check.

Retain source information

Attach the original line number before filtering or grouping rows. Return an error identifying the field whose conversion failed. Source line numbers and offsets describe the evidence to retain.[1]

Trace one record through decode, split and validation

A row "S3,12" can decode as text, parse into a sensor ID and reading, then validate the reading range. The row "S3,-2" reaches the final step if numbers convert correctly. A parser treating every failure as syntax loses why minus two was rejected. See structural and domain validation.

Decode bytes → "S3,-2"
Parse fields → sensor=S3, value=-2
Domain requires value≥0 → validation error
Carry original line number through each step

Test one failure attributable to each phase

A bad byte sequence, missing separator and out-of-range reading each fail differently. Build fixtures that isolate the phase. Preserve raw context in diagnostics in accordance with the application data policy.

References

  1. Python Software Foundation, io: Core Tools for Working with Streams ↩