This Python package is for unpacking basic objects from a text string. The standard data types string, int, float and bool are supported.
A major goal with strup is to provide an intuitive interface. If the standard string methods are too low level and the re-module adds too much complexity to your task, then strup might be your compromise.
Backward compatibility of this API is strongly emphasized.
We apply the utility function unpack(fmt, text) for extracting strings, ints, floats and bools from a text string text. Each character in the string fmt defines the data type for the corresponding object.
>>> from strup import unpack
>>> i, x, s, ok = unpack("ifs?", "5 2.3 ole True")
>>> i, x, s, ok
(5, 2.3, 'ole', True)
Similar syntax is applied in the standard struct module for handling of binary data. However, dots in the fmt string indicates that the corresponding item in text should be ignored.
>>> unpack("f..s", " 2.3 ,ole,55, dole", sep=',') # sep as defined in string.split()
(2.3, ' dole')
Strings confined by quotes are supported
>>> unpack("isf", "100 'Donald Duck' 125.6", quote="'")
(100, 'Donald Duck', 125.6)
Zero-sized string might be interpreted as None objects.
>>> unpack("fissi", "2.3,,, ,12", sep=',', none=True)
(2.3, None, None, ' ', 12)
In loops you may benefit from splitting the grammar and the actual decoding using the class Unpack:
>>> from strup import Unpack
>>> mydecode = Unpack('.s..f', quote='"') # Preprocess the pattern
>>> for line in ['5.3 "Donald Duck" 2 yes 5.4',
'-2.2 "Uncle Sam" 4 no 1.5',
'3.3 "Clint Eastwood" 7 yes 6.5']:
... mydecode(line)
("Donald Duck", 5.4)
("Uncle Sam", 1.5)
("Clint Eastwood", 6.5)
Complete documentation and more examples are hosted on ReadTheDocs.
The source code for this package is located on GitHub.
To install strup from PyPI:
pip install strup # For end users
pip install -e .[dev] # For package development (from the root of your strup Git repo)
or Anaconda:
conda install -c jeblohe strup
strup is continuously tested on Python 2.7, 3.4 and above.
This software is licensed open-source under the MIT License.