April 2018
Intermediate to advanced
408 pages
10h 42m
English
In Chapter 3, Functions, Iterators, and Generators, the Crayola.GPL file was presented without showing the parser. This file looks as follows:
GIMP Palette Name: Crayola Columns: 16 # 239 222 205 Almond 205 149 117 Antique Brass
We can parse a text file using regular expressions. We need to use a filter to read (and parse) header rows. We also want to return an iterable sequence of data rows. This rather complex two-part parsing is based entirely on the two-part—head and tail—file structure.
Following is a low-level parser that handles both the four lines of heading and the long tail:
Head_Body = Tuple[Tuple[str, str], Iterator[List[str]]]def row_iter_gpl(file_obj: TextIO) -> Head_Body: header_pat = re.compile( ...