Parsing

Contents

Parsing#

Three weight classes.

  • Built-ins (str.split, str.partition, json, csv, configparser, tomllib, urllib.parse). First choice when the format is one of these.

  • Regex (re, regex). When the format is line-oriented and the patterns are stable.

  • Real parser (pyparsing, parsy, lark, ANTLR4 Python target). When the grammar is real and the operator needs an AST.

Don’t parse XML or HTML with regex. Use lxml, html.parser, or beautifulsoup4.

References#

  • Strings for regex extraction and substring search.

  • I/O for stdlib parsers (json, csv, tomllib).

  • Regex for the regex DSL.