Install & Compatibility
Where this runs
tested against v1.5.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.8MB
glibcpy 3.10–3.910 runs
installs and imports cleanly · install 1.5s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
scanf
✓ import scanf
Imports the scanf module, allowing access to `scanf.scanf()`.
scanf
✓ from scanf import scanf
Directly imports the `scanf` function, allowing it to be called as `scanf()`.
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.
import scanf
# Example 1: Basic string and integer parsing
name, age = scanf.scanf("Name: %s Age: %d", "Name: Alice Age: 30")
print(f"Parsed - Name: {name}, Age: {age}")
# Example 2: Parsing a more complex string with mixed types
# %[^,] is a scanf-specific specifier meaning 'read until a comma'
input_data = "Product: Laptop, Price: 1200.50, Quantity: 2, ID: xyz123"
format_str = "Product: %[^,], Price: %f, Quantity: %d, ID: %s"
# scanf.scanf returns a tuple of parsed values or None if no match.
result = scanf.scanf(format_str, input_data)
if result:
product, price, quantity, product_id = result
print(f"\nParsed data from complex string:")
print(f" Product: {product}")
print(f" Price: {price:.2f}")
print(f" Quantity: {quantity}")
print(f" ID: {product_id}")
else:
print("\nFailed to parse the input string. Check format string and input data.")
Debug
Known issues
gotchaThe `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.fixEnsure you are running Python 2.7 or Python 3.8 or newer. Use a virtual environment with a compatible Python version if necessary.
affects: <1.6.0 (older versions might have supported more 3.x), 1.6.0
gotchaThe `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.fixRefer to `man scanf` or C `scanf` documentation for format string syntax. Test complex format strings thoroughly to ensure they match your input data.
affects: All versions
gotcha`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.fixAlways 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.
affects: All versions
Errors
Common errors & fixes
ERROR: Package 'scanf' requires Python '>=3.8,==2.7' but the running Python is 3.7.9
Your Python version (e.g., 3.0-3.7) is not supported by the `scanf` library's latest versions.
fixUpgrade your Python environment to 3.8 or newer, or use Python 2.7. Consider using a `venv` or `conda` environment to manage Python versions.
TypeError: scanf() missing 1 required positional argument: 's'
The `scanf` function was called with too few arguments. It expects both a format string and the input string to parse.
fixEnsure you provide two arguments: `scanf.scanf(format_string, input_string)`.
ValueError: invalid literal for int() with base 10: 'not_a_number'
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`).
fixReview 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.
Upgrade
Version history
1.6.0latest on PyPI · released Feb 15, 2025
Audit
Dependencies
No dependency data recorded yet.