Parse
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
- gotcha By default, the `parse` and `search` functions perform case-insensitive matching. If case-sensitive matching is required, you must explicitly set the `case_sensitive=True` argument.
- gotcha The library does not support numbered fields (e.g., `{0}`, `{1}`) like Python's `format()` method. Fields are either anonymous (`{}`) or named (`{field_name}`). The order of anonymous fields in the `Result.fixed` tuple corresponds to their appearance in the pattern.
- gotcha The `compile()` function for `parse` is intentionally not exported when using `from parse import *` to avoid conflicting with Python's built-in `compile()` function. Attempting to use `compile` directly after `import *` will likely call the built-in function, not the library's.
- deprecated The standard library's `parser` module (for accessing Python's internal parse trees) was deprecated in Python 3.9 and removed in Python 3.10. Users often confuse this with the `parse` PyPI library. The `parse` PyPI library is distinct and remains active.
Install
-
pip install parse
Imports
- parse
from parse import parse
- search
from parse import search
- findall
from parse import findall
- with_pattern
from parse import with_pattern
- compile
from parse import compile
Quickstart
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'])})")