{"id":10211,"library":"scanf","title":"Python scanf","description":"The `scanf` library provides a pure-Python implementation of the C-style `scanf` function for parsing strings based on format specifiers. It allows users to extract data of various types from input strings, similar to its C counterpart. The current version is 1.6.0. It has a relatively low release cadence but remains actively maintained with recent commits.","status":"active","version":"1.6.0","language":"en","source_language":"en","source_url":"https://github.com/gwears/scanf","tags":["parsing","string-parsing","utility","c-style"],"install":[{"cmd":"pip install scanf","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"Imports the scanf module, allowing access to `scanf.scanf()`.","symbol":"scanf","correct":"import scanf"},{"note":"Directly imports the `scanf` function, allowing it to be called as `scanf()`.","symbol":"scanf","correct":"from scanf import scanf"}],"quickstart":{"code":"import scanf\n\n# Example 1: Basic string and integer parsing\nname, age = scanf.scanf(\"Name: %s Age: %d\", \"Name: Alice Age: 30\")\nprint(f\"Parsed - Name: {name}, Age: {age}\")\n\n# Example 2: Parsing a more complex string with mixed types\n# %[^,] is a scanf-specific specifier meaning 'read until a comma'\ninput_data = \"Product: Laptop, Price: 1200.50, Quantity: 2, ID: xyz123\"\nformat_str = \"Product: %[^,], Price: %f, Quantity: %d, ID: %s\"\n\n# scanf.scanf returns a tuple of parsed values or None if no match.\nresult = scanf.scanf(format_str, input_data)\n\nif result:\n    product, price, quantity, product_id = result\n    print(f\"\\nParsed data from complex string:\")\n    print(f\"  Product: {product}\")\n    print(f\"  Price: {price:.2f}\")\n    print(f\"  Quantity: {quantity}\")\n    print(f\"  ID: {product_id}\")\nelse:\n    print(\"\\nFailed to parse the input string. Check format string and input data.\")","lang":"python","description":"This quickstart demonstrates how to use `scanf.scanf()` to parse strings. It shows basic type extraction and a more complex scenario using C-style format specifiers, including `%[^,]` to read until a delimiter. It also illustrates checking for `None` as `scanf.scanf` returns `None` on no match."},"warnings":[{"fix":"Ensure you are running Python 2.7 or Python 3.8 or newer. Use a virtual environment with a compatible Python version if necessary.","message":"The `scanf` library explicitly supports Python 2.7 and Python 3.8+ (as per `requires_python ==2.7,>=3.8`). This means Python versions 3.0 through 3.7 are NOT supported and will lead to installation errors or runtime issues.","severity":"gotcha","affected_versions":"<1.6.0 (older versions might have supported more 3.x), 1.6.0"},{"fix":"Refer to `man scanf` or C `scanf` documentation for format string syntax. Test complex format strings thoroughly to ensure they match your input data.","message":"The `scanf` library uses C-style format strings (e.g., `%s`, `%d`, `%f`, `%[^,]`) which can be unfamiliar to Python developers accustomed to f-strings or `.format()`. Incorrect format specifiers, especially for non-standard patterns like `[^,]`, are common sources of parsing errors.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always check the return value of `scanf.scanf()` for `None` before attempting to unpack it. Use an `if result:` block to handle cases where parsing fails gracefully.","message":"`scanf.scanf()` returns `None` if the input string does not fully match the provided format string. It does not raise an exception in this scenario, which can lead to `TypeError: cannot unpack non-iterable NoneType object` if the return value is immediately unpacked.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Upgrade your Python environment to 3.8 or newer, or use Python 2.7. Consider using a `venv` or `conda` environment to manage Python versions.","cause":"Your Python version (e.g., 3.0-3.7) is not supported by the `scanf` library's latest versions.","error":"ERROR: Package 'scanf' requires Python '>=3.8,==2.7' but the running Python is 3.7.9"},{"fix":"Ensure you provide two arguments: `scanf.scanf(format_string, input_string)`.","cause":"The `scanf` function was called with too few arguments. It expects both a format string and the input string to parse.","error":"TypeError: scanf() missing 1 required positional argument: 's'"},{"fix":"Review your format string and the input data. Ensure that the data corresponding to each specifier can be correctly converted to the expected type. Adjust the format string or clean the input data.","cause":"The input data did not match the type expected by the format string (e.g., trying to parse non-numeric text as an integer `%d` or float `%f`).","error":"ValueError: invalid literal for int() with base 10: 'not_a_number'"}]}