Skip to main content

scanf: A small scanf implementation for python

Python has powerful regular expressions but they can be totally overkill for many simpler situations. Additionally, some common numerical formats require quite complex regex's to match them robustly. This python implementation of scanf internally translates the simple scanf format into regular expressions, then returns the parsed values.

Usage

scanf.scanf(format, s=None, collapseWhitespace=True)

Arguments

  • format: This is the format string comprised of plain text and tokens from the table below.
  • s: String to be parsed
  • collapseWhitespace: When True, tells scanf to perform a greedy match with whitespace in the input string, allowing for easy parsing of text that has been formatted to be read more easily. This enables better matching in log files where the data has been formatted for easier reading. These cases have variable amounts of whitespace between the columns, depending on the number of characters in the data itself.

scanf supports the following formats:

Pattern Meaning
%c One character
%5c 5 characters
%d, %i int value
%7d, %7i int value with length 7
%f float value
%o octal value
%X, %x hex value
%s string terminated by whitespace

Any pattern with a * after the % (e.g., '%*f') will result in scanf matching the pattern but omitting the matched portion from the results. This is helpful when parts of the input string may change but should be ignored.

The underlying regex operation is performed using 'search' rather than 'match', so scanf will return a match if the pattern string is matched anywhere in the line.

Examples:

>>> from scanf import scanf
>>> scanf("%s - %d errors, %d warnings", "/usr/sbin/sendmail - 0 errors, 4 warnings")
('/usr/sbin/sendmail', 0, 4)

>>> scanf("%o %x %d", "0123 0x123 123")
(83, 291, 123)

>>> pattern = 'Power: %f [%], %s, Stemp: %f'
>>> text = 'Power:   0.0 [%], Cool, Stemp: 23.73'
>>> scanf(pattern, text)
(0.0, 'Cool', 23.73)

>>> pattern = 'Power: %f [%], %*s, Stemp: %f'   # note the '*' in %*s
>>> scanf(pattern, text)
(0.0, 23.73)

scanf returns a tuple of parsed values if the input pattern is matched, or None if the format does not match.

Other resources

For more information see:

Original (pre-1.0) code from: http://code.activestate.com/recipes/502213-simple-scanf-implementation/

Releases

1.5.1: 2018-10-04

  • Re-added Python 2.7 compatibility via backports.functools_lru_cache (thanks @eendebakpt!)

1.5: 2018-10-01

  • Fixed Python 3.7 compatibility (scanf_compile broke in 3.7 due to differences in re.sub)
  • Changed caching to functools.lru_cache (in Python 3 standard library)
  • Dropping Python 2 support, as lru_cache is not in Python 2 standard library
  • Caching now takes collapseWhitespace into account (thanks @prittenhouse!)

1.4.1: 2017-04-05

  • Added $^| characters to the list of special characters to escape in 'scanf_compile'. Thanks @MichaelWedel!

1.4: 2016-12-03

  • Small modification to scanf.py for Python3 compatibility. Thanks @Gattocrucco!
  • Changed README.md to README.rst, removing pypandoc dependency in setup.py
  • Removed most of the comments at the beginning of scanf.py, as they were redundant with those in the README.

1.3.1 - 1.3.3: 2016-06-23

  • Initial release to PyPI
  • Fixed various issues with metadata for PyPI

1.3: 2016-01-18

  • Added 'extractdata' function.

1.2: 2013-05-30

  • Added 'collapseWhitespace' flag (defaults to True) to take the search string and replace all whitespace with regex string to match repeated whitespace. This enables better matching in log files where the data has been formatted for easier reading. These cases have variable amounts of whitespace between the columns, depending on the number of characters in the data itself.

1.1: 2010-10-13

  • Changed regex from 'match' (only matches at beginning of line) to 'search' (matches anywhere in line)
  • Bugfix - ignore cast for skipped fields

1.0: 2010-10-11

  • Initial release (internal)

Metadata

Release files for scanf 1.5.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for scanf 1.5.1
File Size Uploaded
scanf-1.5.1.tar.gz 5.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for scanf 1.5.1
File Interpreter ABI Platform
scanf-1.5.1-py3-none-any.whl Python 3 none any Details

Total release size: 10.7 kB

Release files / scanf-1.5.1.tar.gz

Download URL scanf-1.5.1.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
9ff95ece94297c0498a45a6fa24d6f40b1b7b3801b17a3fdf58887c0f65c3c5f
BLAKE2b-256 checksum
How to use checksums
b6d21c996d7dd6e17a78a04a88013291e21046e64dd3b38ca1faecd12cd9ccac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.22.0 CPython/3.6.5

Release files / scanf-1.5.1-py3-none-any.whl

Download URL scanf-1.5.1-py3-none-any.whl
Size 5.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ffbc65635e51a32739bc0805860dd30b2463b798eb505544588a98e744d65a0e
BLAKE2b-256 checksum
How to use checksums
95b3cab12d8fe6964a035aea3c31ff37a4a8f18331aea6844b22749aee74d2bd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.22.0 CPython/3.6.5

Release history Release notifications | RSS feed

1.6.0

2 release files

1.5.2

2 release files

This release

1.5.1 This release

2 release files

1.5

2 release files

1.4.1

2 release files

1.4

3 release files

1.3.3

1 release file

1.3.2

1 release file

1.3.1

1 release file

1.3

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page