From 65dd4ae929241c813e7a9b700ead46d68b20821f Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Sat, 12 Apr 2008 23:18:27 +0000 Subject: Fix fromregex, add documentation and tests [patch by Pauli Virtanen]. --- numpy/lib/io.py | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) (limited to 'numpy/lib/io.py') diff --git a/numpy/lib/io.py b/numpy/lib/io.py index d16432814..3903f779e 100644 --- a/numpy/lib/io.py +++ b/numpy/lib/io.py @@ -362,22 +362,44 @@ def savetxt(fname, X, fmt='%.18e',delimiter=' '): X.shape = origShape import re -def fromregex(file, regexp, **kwds): +def fromregex(file, regexp, dtype): """Construct a record array from a text file, using regular-expressions parsing. - Groups in the regular exespression are converted to fields. + Array is constructed from all matches of the regular expression + in the file. Groups in the regular expression are converted to fields. + + Parameters + ---------- + file : str or file + File name or file object to read + regexp : str or regexp + Regular expression to use to parse the file + dtype : dtype or dtype list + Dtype for the record array + + Example + ------- + >>> import numpy as np + >>> f = open('test.dat', 'w') + >>> f.write("1312 foo\n1534 bar\n 444 qux") + >>> f.close() + >>> np.fromregex('test.dat', r"(\d+)\s+(...)", [('num', np.int64), ('key', 'S3')]) + array([(1312L, 'foo'), (1534L, 'bar'), (444L, 'qux')], + dtype=[('num', '