[Numpy-svn] r8180 - trunk/numpy/lib

numpy-svn@scip... numpy-svn@scip...
Sat Feb 20 12:17:29 CST 2010


Author: ptvirtan
Date: 2010-02-20 12:17:29 -0600 (Sat, 20 Feb 2010)
New Revision: 8180

Modified:
   trunk/numpy/lib/io.py
Log:
3K: lib: fix savetxt

This will make savetxt open files in the 'wb' mode on Python 3.  To
allow using any sort of newlines (which are different e.g. on Windows),
add a new 'newline' keyword.

Modified: trunk/numpy/lib/io.py
===================================================================
--- trunk/numpy/lib/io.py	2010-02-20 18:17:14 UTC (rev 8179)
+++ trunk/numpy/lib/io.py	2010-02-20 18:17:29 UTC (rev 8180)
@@ -10,6 +10,7 @@
 import format
 import cStringIO
 import os
+import sys
 import itertools
 import warnings
 from operator import itemgetter
@@ -23,7 +24,7 @@
                      _is_string_like, has_nested_fields, flatten_dtype, \
                      easy_dtype, _bytes_to_name
 
-from numpy.compat import asbytes
+from numpy.compat import asbytes, asstr
 
 _file = open
 _string_like = _is_string_like
@@ -676,7 +677,7 @@
         return X
 
 
-def savetxt(fname, X, fmt='%.18e', delimiter=' '):
+def savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n'):
     """
     Save an array to a text file.
 
@@ -694,7 +695,12 @@
         case `delimiter` is ignored.
     delimiter : str
         Character separating columns.
+    newline : str
+        .. versionadded:: 2.0
 
+        Character separating lines.
+
+
     See Also
     --------
     save : Save an array to a binary file in NumPy ``.npy`` format
@@ -761,12 +767,20 @@
 
     """
 
+    # Py3 conversions first
+    if isinstance(format, bytes):
+        format = asstr(format)
+    delimiter = asbytes(delimiter)
+
     if _is_string_like(fname):
         if fname.endswith('.gz'):
             import gzip
             fh = gzip.open(fname, 'wb')
         else:
-            fh = file(fname, 'w')
+            if sys.version_info[0] >= 3:
+                fh = file(fname, 'wb')
+            else:
+                fh = file(fname, 'w')
     elif hasattr(fname, 'seek'):
         fh = fname
     else:
@@ -792,7 +806,7 @@
     if type(fmt) in (list, tuple):
         if len(fmt) != ncol:
             raise AttributeError('fmt has wrong shape.  %s' % str(fmt))
-        format = delimiter.join(fmt)
+        format = asstr(delimiter).join(map(asstr, fmt))
     elif type(fmt) is str:
         if fmt.count('%') == 1:
             fmt = [fmt, ]*ncol
@@ -804,7 +818,7 @@
             format = fmt
 
     for row in X:
-        fh.write(format % tuple(row) + '\n')
+        fh.write(asbytes(format % tuple(row) + newline))
 
 import re
 def fromregex(file, regexp, dtype):
@@ -1355,11 +1369,11 @@
 #        rows[i] = tuple([convert(val)
 #                         for (convert, val) in zip(conversionfuncs, vals)])
     if loose:
-        rows = zip(*(map(converter._loose_call, map(itemgetter(i), rows))
-                     for (i, converter) in enumerate(converters)))
+        rows = zip(*[map(converter._loose_call, map(itemgetter(i), rows))
+                     for (i, converter) in enumerate(converters)])
     else:
-        rows = zip(*(map(converter._strict_call, map(itemgetter(i), rows))
-                     for (i, converter) in enumerate(converters)))
+        rows = zip(*[map(converter._strict_call, map(itemgetter(i), rows))
+                     for (i, converter) in enumerate(converters)])
     # Reset the dtype
     data = rows
     if dtype is None:



More information about the Numpy-svn mailing list