scanf


Namescanf JSON
Version 1.5.2 PyPI version JSON
download
home_pagehttps://github.com/joshburnett/scanf
SummaryA small scanf implementation
upload_time2018-10-04 16:38:17
maintainer
docs_urlNone
authorJosh Burnett
requires_python
licenseMIT
keywords scanf
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            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:

- http://en.wikipedia.org/wiki/Scanf
- https://github.com/joshburnett/scanf

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


Releases
--------

### 1.5.1: 2018-10-04

- Fixed installation issue 

### 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)




            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/joshburnett/scanf",
    "name": "scanf",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "scanf",
    "author": "Josh Burnett",
    "author_email": "scanf@burnettsonline.org",
    "download_url": "https://files.pythonhosted.org/packages/91/13/670f3c653a8b7cdbfa9ad4957bb140de67533f5778f6dbb6f0a360c04df6/scanf-1.5.2.tar.gz",
    "platform": "",
    "description": "scanf: A small scanf implementation for python\n==============================================\n\nPython has powerful regular expressions but they can be totally\noverkill for many simpler situations. Additionally, some common\nnumerical formats require quite complex regex's to match them\nrobustly. This python implementation of scanf internally translates the simple\nscanf format into regular expressions, then returns the parsed values.\n\nUsage\n-------------\n\nscanf.scanf(format, s=None, collapseWhitespace=True)\n\n*Arguments*\n\n- **format:** This is the format string comprised of plain text and tokens from the\n  table below.\n- **s:** String to be parsed\n- **collapseWhitespace:** When True, tells scanf to perform a greedy match with\n  whitespace in the input string, allowing for easy parsing of text that has\n  been formatted to be read more easily. This enables better matching in log files where the data\n  has been formatted for easier reading. These cases have variable\n  amounts of whitespace between the columns, depending on the number of\n  characters in the data itself.\n\n\nscanf supports the following formats:\n\n| Pattern  | Meaning                                  |\n| :------- | :--------------------------------------- |\n| %c       | One character                            |\n| %5c      | 5 characters                             |\n| %d, %i   | int value                                |\n| %7d, %7i | int value with length 7                  |\n| %f       | float value                              |\n| %o       | octal value                              |\n| %X, %x   | hex value                                |\n| %s       | string terminated by whitespace          |\n\nAny pattern with a * after the % (*e.g.*, '%*f') will result in scanf matching the pattern but\nomitting the matched portion from the results.  This is helpful when parts of\nthe input string may change but should be ignored.\n\nThe underlying regex operation is performed using 'search' rather than 'match',\nso scanf will return a match if the pattern string is matched anywhere in the line.\n\n\n*Examples:*\n\n```\n>>> from scanf import scanf\n>>> scanf(\"%s - %d errors, %d warnings\", \"/usr/sbin/sendmail - 0 errors, 4 warnings\")\n('/usr/sbin/sendmail', 0, 4)\n\n>>> scanf(\"%o %x %d\", \"0123 0x123 123\")\n(83, 291, 123)\n\n>>> pattern = 'Power: %f [%], %s, Stemp: %f'\n>>> text = 'Power:   0.0 [%], Cool, Stemp: 23.73'\n>>> scanf(pattern, text)\n(0.0, 'Cool', 23.73)\n\n>>> pattern = 'Power: %f [%], %*s, Stemp: %f'   # note the '*' in %*s\n>>> scanf(pattern, text)\n(0.0, 23.73)\n```\n\nscanf returns a tuple of parsed values if the input pattern is matched, or None if the format does not match.\n\n\nOther resources\n---------------------\n\nFor more information see:\n\n- http://en.wikipedia.org/wiki/Scanf\n- https://github.com/joshburnett/scanf\n\nOriginal (pre-1.0) code from:\nhttp://code.activestate.com/recipes/502213-simple-scanf-implementation/\n\n\nReleases\n--------\n\n### 1.5.1: 2018-10-04\n\n- Fixed installation issue \n\n### 1.5.1: 2018-10-04\n\n- Re-added Python 2.7 compatibility via backports.functools_lru_cache (thanks @eendebakpt!)\n\n### 1.5: 2018-10-01\n\n- Fixed Python 3.7 compatibility (scanf_compile broke in 3.7 due to differences in re.sub)\n- Changed caching to functools.lru_cache (in Python 3 standard library)\n- Dropping Python 2 support, as lru_cache is not in Python 2 standard library\n- Caching now takes collapseWhitespace into account (thanks @prittenhouse!)\n\n### 1.4.1: 2017-04-05\n\n- Added $^| characters to the list of special characters to escape in 'scanf_compile'. Thanks @MichaelWedel!\n\n### 1.4: 2016-12-03\n\n- Small modification to scanf.py for Python3 compatibility. Thanks @Gattocrucco!\n- Changed README.md to README.rst, removing pypandoc dependency in setup.py\n- Removed most of the comments at the beginning of scanf.py, as they were\n  redundant with those in the README.\n\n### 1.3.1 - 1.3.3: 2016-06-23\n\n- Initial release to PyPI\n- Fixed various issues with metadata for PyPI\n\n### 1.3: 2016-01-18\n\n- Added 'extractdata' function.\n\n### 1.2: 2013-05-30\n\n- Added 'collapseWhitespace' flag (defaults to True) to take the search\n  string and replace all whitespace with regex string to match repeated\n  whitespace. This enables better matching in log files where the data\n  has been formatted for easier reading. These cases have variable\n  amounts of whitespace between the columns, depending on the number of\n  characters in the data itself.\n\n### 1.1: 2010-10-13\n\n- Changed regex from 'match' (only matches at beginning of line) to\n  'search' (matches anywhere in line)\n- Bugfix - ignore cast for skipped fields\n\n### 1.0: 2010-10-11\n\n- Initial release (internal)\n\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A small scanf implementation",
    "version": "1.5.2",
    "project_urls": {
        "Homepage": "https://github.com/joshburnett/scanf"
    },
    "split_keywords": [
        "scanf"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4f88e9033395e10da921c7fd6a2062f3289674f80dbbf7747214ac4beee498cd",
                "md5": "2c552a5fb506350377a1cc01ad4eb1ac",
                "sha256": "905c004a6aed96f53ff82d41550fe57b2185685ce575116ce58c13c2998c52ac"
            },
            "downloads": -1,
            "filename": "scanf-1.5.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "2c552a5fb506350377a1cc01ad4eb1ac",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 5408,
            "upload_time": "2018-10-04T16:38:16",
            "upload_time_iso_8601": "2018-10-04T16:38:16.326892Z",
            "url": "https://files.pythonhosted.org/packages/4f/88/e9033395e10da921c7fd6a2062f3289674f80dbbf7747214ac4beee498cd/scanf-1.5.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9113670f3c653a8b7cdbfa9ad4957bb140de67533f5778f6dbb6f0a360c04df6",
                "md5": "053b549a471feaa48d5b14ba991764a4",
                "sha256": "57633440a02a138cd14b693d09270af0a03bb017e8d4cfd248c7988b31b8cb81"
            },
            "downloads": -1,
            "filename": "scanf-1.5.2.tar.gz",
            "has_sig": false,
            "md5_digest": "053b549a471feaa48d5b14ba991764a4",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 5477,
            "upload_time": "2018-10-04T16:38:17",
            "upload_time_iso_8601": "2018-10-04T16:38:17.268236Z",
            "url": "https://files.pythonhosted.org/packages/91/13/670f3c653a8b7cdbfa9ad4957bb140de67533f5778f6dbb6f0a360c04df6/scanf-1.5.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2018-10-04 16:38:17",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "joshburnett",
    "github_project": "scanf",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "scanf"
}
        
Elapsed time: 0.20847s