{"id":1007,"library":"parse","title":"Parse","description":"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.","status":"active","version":"1.21.1","language":"python","source_language":"en","source_url":"https://github.com/r1chardj0n3s/parse","tags":["parsing","string parsing","text processing","format string","reverse format"],"install":[{"cmd":"pip install parse","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"symbol":"parse","correct":"from parse import parse"},{"symbol":"search","correct":"from parse import search"},{"symbol":"findall","correct":"from parse import findall"},{"symbol":"with_pattern","correct":"from parse import with_pattern"},{"note":"The `compile` function is explicitly excluded from `from parse import *` to avoid overriding Python's built-in `compile()` function. It must be imported directly or accessed via `parse.compile` if doing `import parse`.","wrong":"from parse import *; compile(...)","symbol":"compile","correct":"from parse import compile"}],"quickstart":{"code":"from parse import parse, search, findall, compile\n\n# Basic parsing\nresult = parse(\"Hello {name}!\", \"Hello World!\")\nif result: # Check if parsing was successful\n    print(f\"Name (basic): {result['name']}\")\n\n# Named fields\nlog_line = \"User {user_id} logged in from {ip_address} at {timestamp}\"\nparsed_log = parse(log_line, \"User 123 logged in from 192.168.1.1 at 2023-01-01 10:00:00\")\nif parsed_log:\n    print(f\"User ID: {parsed_log['user_id']}, IP: {parsed_log['ip_address']}\")\n\n# Searching for patterns\ntext = \"The quick brown fox jumps over the lazy dog. Another fox is here.\"\nfound = search(\"fox\", text)\nif found:\n    print(f\"Found 'fox' at position: {found.span}\")\n\n# Finding all occurrences\nall_foxes = findall(\"fox\", text)\nprint(f\"All 'fox' matches: {[m.span for m in all_foxes]}\")\n\n# Compiling a pattern for efficiency\nparser = compile(\"Sensor {sensor_id} reading: {value:g}\")\nsensor_data = \"Sensor A1 reading: 25.5\\nSensor B2 reading: 99.123\"\n\nfor line in sensor_data.split('\\n'):\n    data = parser.parse(line)\n    if data:\n        print(f\"Compiled - Sensor ID: {data['sensor_id']}, Value: {data['value']} (Type: {type(data['value'])})\")\n","lang":"python","description":"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."},"warnings":[{"fix":"Use `parse(pattern, text, case_sensitive=True)` or `search(pattern, text, case_sensitive=True)`.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Use named fields (`{field_name}`) for clarity or rely on the positional order of anonymous fields. Avoid attempting to use numbered field syntax.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Import `compile` explicitly (e.g., `from parse import compile`) or import the module directly (e.g., `import parse` and then use `parse.compile()`).","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"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.","severity":"deprecated","affected_versions":"Python 3.9 (deprecated), Python 3.10+ (removed)"},{"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.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Use `Result.spans` (plural) to access the span information for matched fields.","message":"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`.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-05-12T22:31:48.616Z","next_check":"2026-06-27T00:00:00.000Z","problems":[{"fix":"Install the library using pip: `pip install 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.","error":"ModuleNotFoundError: No module named 'parse'"},{"fix":"Access the matched data using `result.fixed` for positional matches or `result.named` for named matches.\n\nExample:\n```python\nimport parse\nresult = parse.parse(\"Hello {}\", \"Hello World\")\n# print(result.group(0)) # Incorrect\nprint(result.fixed)   # Correct\n```","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.","error":"AttributeError: 'Result' object has no attribute 'group'"},{"fix":"Ensure the input string's data matches the expected type in the format specification, or handle the `ValueError` with a `try-except` block.\n\nExample:\n```python\nimport parse\n# result = parse.parse(\"ID: {:d}\", \"ID: not_a_number\") # Causes ValueError\ntry:\n    result = parse.parse(\"ID: {:d}\", \"ID: 123\")\n    print(result.fixed)\nexcept ValueError as e:\n    print(f\"Parsing failed: {e}\")\n```","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.","error":"ValueError: invalid literal for int() with base 10: 'not_a_number'"},{"fix":"Either call the function using `parse.parse(...)` or change the import statement to `from parse import parse`.\n\nExample:\n```python\nimport parse\n# result = parse(\"Item: {}\", \"Item: Apple\") # Causes NameError\nresult = parse.parse(\"Item: {}\", \"Item: Apple\") # Correct\nprint(result.fixed)\n\n# Alternative fix:\n# from parse import parse\n# result = parse(\"Item: {}\", \"Item: Apple\") # This now works\n# print(result.fixed)\n```","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(\"...\")`).","error":"NameError: name 'parse' is not defined"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"install_tag":"verified","quickstart_score":null,"quickstart_tag":null,"pypi_latest":"1.22.0","cli_name":"","install_checks":{"last_tested":"2026-05-12","tag":"verified","tag_description":"installs cleanly on critical runtimes, fast import, recently tested","results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":0.02,"mem_mb":1.3,"disk_size":"17.9M"},{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.02,"mem_mb":1.3,"disk_size":"17.9M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.5,"import_time_s":0.01,"mem_mb":1.3,"disk_size":"18M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":1.3,"disk_size":"18M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":0.04,"mem_mb":1.5,"disk_size":"19.7M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.05,"mem_mb":1.5,"disk_size":"19.7M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.6,"import_time_s":0.04,"mem_mb":1.5,"disk_size":"20M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.04,"mem_mb":1.5,"disk_size":"20M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":0.03,"mem_mb":1.1,"disk_size":"11.6M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.03,"mem_mb":1.1,"disk_size":"11.6M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.4,"import_time_s":0.03,"mem_mb":1.1,"disk_size":"12M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.03,"mem_mb":1.1,"disk_size":"12M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":0.03,"mem_mb":1.4,"disk_size":"11.3M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.03,"mem_mb":1.4,"disk_size":"11.2M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.4,"import_time_s":0.03,"mem_mb":1.2,"disk_size":"12M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.03,"mem_mb":1.2,"disk_size":"12M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":0.02,"mem_mb":1.1,"disk_size":"17.4M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.02,"mem_mb":1.1,"disk_size":"17.4M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.7,"import_time_s":0.01,"mem_mb":1.1,"disk_size":"18M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.02,"mem_mb":1.1,"disk_size":"18M"}]},"quickstart_checks":{"last_tested":"2026-04-24","tag":null,"tag_description":null,"results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":1}]}}