Parse

raw JSON →
1.21.1 verified Tue May 12 auth: no python install: verified

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.

pip install parse
error ModuleNotFoundError: No module named 'parse'
cause The 'parse' library is not installed in the Python environment, or the environment where it is installed is not the one being used.
fix
Install the library using pip: pip install parse
error AttributeError: 'Result' object has no attribute 'group'
cause The user is attempting to access parsed data using a method (like `group()`) commonly found on `re.Match` objects, but the `parse` library's `Result` object stores extracted data in its `fixed` (tuple) or `named` (dictionary) attributes.
fix
Access the matched data using result.fixed for positional matches or result.named for named matches. Example:
import parse
result = parse.parse("Hello {}", "Hello World")
# print(result.group(0)) # Incorrect
print(result.fixed)   # Correct
error ValueError: invalid literal for int() with base 10: 'not_a_number'
cause The format string used with `parse()` specifies a type conversion (e.g., `:d` for integer), but the corresponding part of the input string cannot be successfully converted to that type.
fix
Ensure the input string's data matches the expected type in the format specification, or handle the ValueError with a try-except block. Example:
import parse
# result = parse.parse("ID: {:d}", "ID: not_a_number") # Causes ValueError
try:
    result = parse.parse("ID: {:d}", "ID: 123")
    print(result.fixed)
except ValueError as e:
    print(f"Parsing failed: {e}")
error NameError: name 'parse' is not defined
cause The 'parse' module was imported using `import parse`, but the user attempted to call the `parse` function directly (e.g., `parse("...")`) without qualifying it with the module name (`parse.parse("...")`).
fix
Either call the function using parse.parse(...) or change the import statement to from parse import parse. Example:
import parse
# result = parse("Item: {}", "Item: Apple") # Causes NameError
result = parse.parse("Item: {}", "Item: Apple") # Correct
print(result.fixed)

# Alternative fix:
# from parse import parse
# result = parse("Item: {}", "Item: Apple") # This now works
# print(result.fixed)
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.
fix Use `parse(pattern, text, case_sensitive=True)` or `search(pattern, text, case_sensitive=True)`.
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.
fix Use named fields (`{field_name}`) for clarity or rely on the positional order of anonymous fields. Avoid attempting to use numbered field syntax.
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.
fix Import `compile` explicitly (e.g., `from parse import compile`) or import the module directly (e.g., `import parse` and then use `parse.compile()`).
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.
fix Ensure you are installing and importing the `parse` PyPI library (`pip install parse`, `from parse import parse`). If you intended to parse Python code's AST, use the `ast` module instead of the removed `parser` module.
gotcha The `Result` object returned by `parse()` or `search()` does not have a `span` attribute. Unlike Python's `re` module `MatchObject`, the `parse` library's `Result` objects primarily provide access to the matched fields themselves, not their start and end character positions within the original text.
fix Access matched data through named fields (e.g., `result.field_name`) or positional access (e.g., `result[0]`, `result.fixed`). If character span information (start and end indices of the match) is required, consider using Python's built-in `re` module, which provides `span()` on its match objects.
gotcha The `Result` object returned by `parse` and `search` functions provides a `spans` attribute (plural) which is a list of (start, end) tuples for matched fields. It does not have a `span` (singular) attribute. Attempting to access `Result.span` will raise an `AttributeError`.
fix Use `Result.spans` (plural) to access the span information for matched fields.
python os / libc status wheel install import disk
3.10 alpine (musl) wheel - 0.02s 17.9M
3.10 alpine (musl) - - 0.02s 17.9M
3.10 slim (glibc) wheel 1.5s 0.01s 18M
3.10 slim (glibc) - - 0.01s 18M
3.11 alpine (musl) wheel - 0.04s 19.7M
3.11 alpine (musl) - - 0.05s 19.7M
3.11 slim (glibc) wheel 1.6s 0.04s 20M
3.11 slim (glibc) - - 0.04s 20M
3.12 alpine (musl) wheel - 0.03s 11.6M
3.12 alpine (musl) - - 0.03s 11.6M
3.12 slim (glibc) wheel 1.4s 0.03s 12M
3.12 slim (glibc) - - 0.03s 12M
3.13 alpine (musl) wheel - 0.03s 11.3M
3.13 alpine (musl) - - 0.03s 11.2M
3.13 slim (glibc) wheel 1.4s 0.03s 12M
3.13 slim (glibc) - - 0.03s 12M
3.9 alpine (musl) wheel - 0.02s 17.4M
3.9 alpine (musl) - - 0.02s 17.4M
3.9 slim (glibc) wheel 1.7s 0.01s 18M
3.9 slim (glibc) - - 0.02s 18M

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'])})")