Python scanf
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.
Common errors
-
ERROR: Package 'scanf' requires Python '>=3.8,==2.7' but the running Python is 3.7.9
cause 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'
cause 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'
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`).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.
Warnings
- gotcha 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.
- gotcha 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.
- 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.
Install
-
pip install scanf
Imports
- scanf
import scanf
- scanf
from scanf import scanf
Quickstart
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.")