Parse

1.21.1 · active · verified Sun Mar 29

The `parse` library is a lightweight Python module designed to parse strings using a specification based on Python's built-in `format()` syntax. It effectively acts as the opposite of `format()`, enabling easy extraction of data from structured text. As of version 1.21.1, it is stable and widely used for simple text parsing tasks, offering a more readable alternative to regular expressions for many common patterns. It doesn't follow a strict release cadence but is actively maintained.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic string parsing, extracting named fields, searching for patterns, finding all occurrences, and compiling a pattern for repeated use, which improves performance. It also shows how to use type specifiers like `:g` for floats.

from parse import parse, search, findall, compile

# Basic parsing
result = parse("Hello {name}!", "Hello World!")
if result: # Check if parsing was successful
    print(f"Name (basic): {result['name']}")

# Named fields
log_line = "User {user_id} logged in from {ip_address} at {timestamp}"
parsed_log = parse(log_line, "User 123 logged in from 192.168.1.1 at 2023-01-01 10:00:00")
if parsed_log:
    print(f"User ID: {parsed_log['user_id']}, IP: {parsed_log['ip_address']}")

# Searching for patterns
text = "The quick brown fox jumps over the lazy dog. Another fox is here."
found = search("fox", text)
if found:
    print(f"Found 'fox' at position: {found.span}")

# Finding all occurrences
all_foxes = findall("fox", text)
print(f"All 'fox' matches: {[m.span for m in all_foxes]}")

# Compiling a pattern for efficiency
parser = compile("Sensor {sensor_id} reading: {value:g}")
sensor_data = "Sensor A1 reading: 25.5\nSensor B2 reading: 99.123"

for line in sensor_data.split('\n'):
    data = parser.parse(line)
    if data:
        print(f"Compiled - Sensor ID: {data['sensor_id']}, Value: {data['value']} (Type: {type(data['value'])})")

view raw JSON →