liveBook · Manning

Delimiter

As explained in The Quick Python Book, Fourth Edition

A separator is a formatting character inserted between groups of digits in a structured text value.

A delimiter marks a boundary in structured text. In a flat file, it separates fields within a record: `Illinois|1979/01/01|17.48|994` has four pipe-delimited fields. Commas and tabs are also common choices. A delimiter works best when it is not expected inside a field value. section 21.2.3

Split a simple record

`"Illinois|1979/01/01|17.48|994".split("|")` returns `['Illinois', '1979/01/01', '17.48', '994']`. The fields remain strings; convert values such as temperatures or counts separately when needed. section 21.2.3

Python’s `split()` uses runs of whitespace when no delimiter is supplied. Pass a specific string to split on that sequence instead. Its optional second argument limits the number of splits, letting the final field contain otherwise troublesome text. To build a string, call `join()` on the desired separator: `"::".join(["Separated", "with", "colons"])` produces `Separated::with::colons`. section 6.4.1

For substantial delimited-file work, prefer the standard-library `csv` module to splitting every line yourself. Set `delimiter="|"` when reading a pipe-delimited file. Quoted fields can contain the delimiting character without ending the field; the module also handles that case when writing. A plain `split(",")` cannot make that distinction. section 21.2.4 section 21.5.1

Separators also appear between groups of digits, as in phone numbers. When normalizing phone numbers with several possible separator styles, matching only a few styles can miss valid inputs. If the input is known to be a phone number, one approach is to extract the digits, validate them, then insert the required separators. section 16.6.2

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Works through string splitting and joining, then addresses quoted-field handling with the csv module rather than implementing a full parser.
Examples
Uses pipe-delimited temperature records, whitespace and substring splitting, and phone numbers with varied separators.
What it assumes
Assumes basic Python string and file operations while demonstrating how to specify delimiters in string methods and csv.reader.
What you can do after
Apply it via Python split and join for simple text, or the csv module for delimited files with quoted fields.
How it connects
Connects delimiters to string methods, flat files, CSV, quoted fields, and regular-expression-based phone-number normalization.

Also explained in

Compare how 11 books explain delimiter →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.