summaryrefslogtreecommitdiff
path: root/numpy/lib
diff options
context:
space:
mode:
authordhuard <dhuard@localhost>2008-04-16 14:28:11 +0000
committerdhuard <dhuard@localhost>2008-04-16 14:28:11 +0000
commit13f9b4a8fb920c90d9f57de074794ea0c5782f10 (patch)
tree6e8b3890501fc2c0ffdb68abad7f2e942412d1d2 /numpy/lib
parent3b6397f2e992c5f061e0b954681443f989f7a7d7 (diff)
downloadnumpy-13f9b4a8fb920c90d9f57de074794ea0c5782f10.tar.gz
Added and fixed some tests for loadtxt and savetxt. Cleaned up the docstring of savetxt, added some info on formatting.
Diffstat (limited to 'numpy/lib')
-rw-r--r--numpy/lib/io.py59
-rw-r--r--numpy/lib/tests/test_io.py84
2 files changed, 124 insertions, 19 deletions
diff --git a/numpy/lib/io.py b/numpy/lib/io.py
index 3903f779e..6edf902e3 100644
--- a/numpy/lib/io.py
+++ b/numpy/lib/io.py
@@ -323,19 +323,52 @@ def savetxt(fname, X, fmt='%.18e',delimiter=' '):
Save the data in X to file fname using fmt string to convert the
data to strings
- fname can be a filename or a file handle. If the filename ends in .gz,
- the file is automatically saved in compressed gzip format. The load()
- command understands gzipped files transparently.
-
- Example usage:
-
- save('test.out', X) # X is an array
- save('test1.out', (x,y,z)) # x,y,z equal sized 1D arrays
- save('test2.out', x) # x is 1D
- save('test3.out', x, fmt='%1.4e') # use exponential notation
-
- delimiter is used to separate the fields, eg delimiter ',' for
- comma-separated values
+ Parameters
+ ----------
+ fname : filename or a file handle
+ If the filename ends in .gz, the file is automatically saved in
+ compressed gzip format. The load() command understands gzipped files
+ transparently.
+ X : array or sequence
+ Data to write to file.
+ fmt : string
+ A format string %[flags][width][.precision]specifier. See notes below for
+ a description of some common flags and specifiers.
+ delimiter : str
+ Character separating columns.
+
+ Examples
+ --------
+ >>> savetxt('test.out', x, delimiter=',') # X is an array
+ >>> savetxt('test.out', (x,y,z)) # x,y,z equal sized 1D arrays
+ >>> savetxt('test.out', x, fmt='%1.4e') # use exponential notation
+
+ Notes on fmt
+ ------------
+ flags:
+ - : left justify
+ + : Forces to preceed result with + or -.
+ 0 : Left pad the number with zeros instead of space (see width).
+ width:
+ Minimum number of characters to be printed. The value is not truncated.
+ precision:
+ For integer specifiers (eg. d,i,o,x), the minimum number of digits.
+ For e, E and f specifiers, the number of digits to print after the decimal
+ point.
+ For g and G, the maximum number of significant digits.
+ For s, the maximum number of characters.
+ specifiers:
+ c : character
+ d or i : signed decimal integer
+ e or E : scientific notation with e or E.
+ f : decimal floating point
+ g,G : use the shorter of e,E or f
+ o : signed octal
+ s : string of characters
+ u : unsigned decimal integer
+ x,X : unsigned hexadecimal integer
+
+ This is not an exhaustive specification.
"""
if _string_like(fname):
diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py
index 9f7a585ef..9e398cf23 100644
--- a/numpy/lib/tests/test_io.py
+++ b/numpy/lib/tests/test_io.py
@@ -14,24 +14,54 @@ class TestSaveTxt(NumpyTestCase):
a =np.array( [[1,2],[3,4]], int)
c = StringIO.StringIO()
- np.savetxt(c, a)
+ np.savetxt(c, a, fmt='%d')
c.seek(0)
- assert(c.readlines(), ['1 2\n', '3 4\n'])
+ assert_equal(c.readlines(), ['1 2\n', '3 4\n'])
def test_1D(self):
a = np.array([1,2,3,4], int)
c = StringIO.StringIO()
np.savetxt(c, a, fmt='%d')
c.seek(0)
- assert(c.readlines(), ['1\n', '2\n', '3\n', '4\n'])
+ lines = c.readlines()
+ assert_equal(lines, ['1\n', '2\n', '3\n', '4\n'])
def test_record(self):
a = np.array([(1, 2), (3, 4)], dtype=[('x', 'i4'), ('y', 'i4')])
c = StringIO.StringIO()
np.savetxt(c, a, fmt='%d')
c.seek(0)
- assert(c.readlines(), ['1 2\n', '3 4\n'])
-
+ assert_equal(c.readlines(), ['1 2\n', '3 4\n'])
+ def test_delimiter(self):
+ a = np.array([[1., 2.], [3., 4.]])
+ c = StringIO.StringIO()
+ np.savetxt(c, a, delimiter=',', fmt='%d')
+ c.seek(0)
+ assert_equal(c.readlines(), ['1,2\n', '3,4\n'])
+
+
+## def test_format(self):
+## a = np.array([(1, 2), (3, 4)])
+## c = StringIO.StringIO()
+## # Sequence of formats
+## np.savetxt(c, a, fmt=['%02d', '%3.1f'])
+## c.seek(0)
+## assert_equal(c.readlines(), ['01 2.0\n', '03 4.0\n'])
+##
+## # A single multiformat string
+## c = StringIO.StringIO()
+## np.savetxt(c, a, fmt='%02d : %3.1f')
+## c.seek(0)
+## lines = c.readlines()
+## assert_equal(lines, ['01 : 2.0\n', '03 : 4.0\n'])
+##
+## # Specify delimiter, should be overiden
+## c = StringIO.StringIO()
+## np.savetxt(c, a, fmt='%02d : %3.1f', delimiter=',')
+## c.seek(0)
+## lines = c.readlines()
+## assert_equal(lines, ['01 : 2.0\n', '03 : 4.0\n'])
+
class TestLoadTxt(NumpyTestCase):
def test_record(self):
@@ -45,7 +75,6 @@ class TestLoadTxt(NumpyTestCase):
d = StringIO.StringIO()
d.write('M 64.0 75.0\nF 25.0 60.0')
d.seek(0)
-
mydescriptor = {'names': ('gender','age','weight'),
'formats': ('S1',
'i4', 'f4')}
@@ -54,6 +83,7 @@ class TestLoadTxt(NumpyTestCase):
y = np.loadtxt(d, dtype=mydescriptor)
assert_array_equal(y, b)
+
def test_array(self):
c = StringIO.StringIO()
c.write('1 2\n3 4')
@@ -92,6 +122,48 @@ class TestLoadTxt(NumpyTestCase):
converters={3:lambda s: int(s or -999)})
a = np.array([1,2,3,-999,5], int)
assert_array_equal(x, a)
+
+ def test_comments(self):
+ c = StringIO.StringIO()
+ c.write('# comment\n1,2,3,5\n')
+ c.seek(0)
+ x = np.loadtxt(c, dtype=int, delimiter=',', \
+ comments='#')
+ a = np.array([1,2,3,5], int)
+ assert_array_equal(x, a)
+
+ def test_skiprows(self):
+ c = StringIO.StringIO()
+ c.write('comment\n1,2,3,5\n')
+ c.seek(0)
+ x = np.loadtxt(c, dtype=int, delimiter=',', \
+ skiprows=1)
+ a = np.array([1,2,3,5], int)
+ assert_array_equal(x, a)
+
+ c = StringIO.StringIO()
+ c.write('# comment\n1,2,3,5\n')
+ c.seek(0)
+ x = np.loadtxt(c, dtype=int, delimiter=',', \
+ skiprows=1)
+ a = np.array([1,2,3,5], int)
+ assert_array_equal(x, a)
+
+ def test_usecols(self):
+ a =np.array( [[1,2],[3,4]], float)
+ c = StringIO.StringIO()
+ np.savetxt(c, a)
+ c.seek(0)
+ x = np.loadtxt(c, dtype=float, usecols=(1,))
+ assert_array_equal(x, a[:,1])
+
+ a =np.array( [[1,2,3],[3,4,5]], float)
+ c = StringIO.StringIO()
+ np.savetxt(c, a)
+ c.seek(0)
+ x = np.loadtxt(c, dtype=float, usecols=(1,2))
+ assert_array_equal(x, a[:,1:])
+
class Testfromregex(NumpyTestCase):
def test_record(self):