Deviopath › Python

Parse a Config String

Medium · Parsing

Three string methods do most small parsing jobs: split(sep) cuts a string into parts, strip() removes spaces at both ends, and partition("=") splits at the FIRST = only and gives you (before, "=", after).

Write parse_config(text) that turns a string like "host=localhost; port=5432" into a dict of strings. Parts are separated by ;. Strip spaces around keys and values, and skip blank parts. Split each part at the first = only, because values may contain =. If a key appears twice, the last value wins.

Next: Top K Most Common Words