diff options
author | Travis Oliphant <oliphant@enthought.com> | 2008-04-04 06:11:24 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2008-04-04 06:11:24 +0000 |
commit | effc09b20ea3146d3dcfc6884b4321ddf1638cb5 (patch) | |
tree | b730047a85df6b1c374c473979e6614c27692b50 /numpy/lib/io.py | |
parent | d396f126cc9ac2f215c1363a8f8650d6c4e6c161 (diff) | |
download | numpy-effc09b20ea3146d3dcfc6884b4321ddf1638cb5.tar.gz |
Add fromregex function (needs more testing) and some simple spreadsheet-like financial calculations.
Diffstat (limited to 'numpy/lib/io.py')
-rw-r--r-- | numpy/lib/io.py | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/numpy/lib/io.py b/numpy/lib/io.py index 7ef7df933..bb4d254a8 100644 --- a/numpy/lib/io.py +++ b/numpy/lib/io.py @@ -3,6 +3,7 @@ __all__ = ['savetxt', 'loadtxt', 'load', 'loads', 'save', 'savez', 'packbits', 'unpackbits', + 'fromregex', 'DataSource'] import numpy as np @@ -361,3 +362,24 @@ def savetxt(fname, X, fmt='%.18e',delimiter=' '): if origShape is not None: X.shape = origShape + +import re +def fromregex(file, regexp, **kwds): + """Construct a record array from a text file, using regular-expressions parsing. + + Groups in the regular exespression are converted to fields. + """ + if not hasattr(file, "read"): + file = open(file,'r') + if not hasattr(regexp, 'match'): + regexp = re.compile(regexp) + + seq = regexp.findall(file.read()) + dtypelist = [] + for key, value in kwds.values(): + dtypelist.append((key, value)) + format = np.dtype(dtypelist) + output = array(seq, dtype=format) + return output + + |