[Numpy-tickets] [NumPy] #663: Support for multi formatting elements in savetxt

NumPy numpy-tickets@scipy....
Wed Apr 16 09:46:13 CDT 2008


#663: Support for multi formatting elements in savetxt
-------------------------+--------------------------------------------------
 Reporter:  dhuard       |        Owner:  somebody
     Type:  enhancement  |       Status:  new     
 Priority:  normal       |    Milestone:  1.0.5   
Component:  Other        |      Version:  none    
 Severity:  normal       |   Resolution:          
 Keywords:               |  
-------------------------+--------------------------------------------------
Comment (by dhuard):

 To increase the probability of someone reviewing this, I tried to simplify
 the process. I committed the part of the patch related to docstring
 enhancements as well as fixes and additions to the tests (r5037). The test
 for multi-format is simply commented out in the test_io.py file. Here is
 the slimmed version of the patch for multi-format support in io.py. Simply
 uncomment the tests related to format in test_io.py to make sure
 everything works as advertised.

 {{{
 Index: io.py
 ===================================================================
 --- io.py       (revision 5037)
 +++ io.py       (working copy)
 @@ -316,7 +316,6 @@
      else:  return X


 -# adjust so that fmt can change across columns if desired.

  def savetxt(fname, X, fmt='%.18e',delimiter=' '):
      """
 @@ -331,9 +330,9 @@
        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.
 +    fmt : format string or sequence of format strings
 +      A single format (%10.5f), a sequence of formats, or a multi-format
 string,
 +      e.g. 'Iteration %d -- %10.5f', in which case delimiter is ignored.
      delimiter : str
        Character separating columns.

 @@ -382,18 +381,34 @@
      else:
          raise ValueError('fname must be a string or file handle')

 +    X = np.asarray(X)
 +    if X.ndim == 1:
 +        if X.dtype.names is None:
 +            X = np.atleast_2d(X).T
 +            ncol = 1
 +        else:
 +            ncol = len(X.dtype.descr)
 +    else:
 +        ncol = X.shape[1]
 +
 +    # Fmt can be a string with multiple insertion points or a list of
 formats.
 +    # E.g. '%10.5f\t%10d' or ('%10.5f', '$10d')
 +    if type(fmt) in (list, tuple):
 +        if len(fmt) != ncol:
 +            raise AttributeError, 'fmt has wrong shape.  '+ str(fmt)
 +        format = delimiter.join(fmt)
 +    elif type(fmt) is str:
 +        if fmt.count('%') == 1:
 +            fmt = [fmt,]*ncol
 +            format = delimiter.join(fmt)
 +        elif fmt.count('%') != ncol:
 +            raise AttributeError, 'fmt has wrong number of % formats.  '
 + fmt
 +        else:
 +            format = fmt

 -    X = np.asarray(X)
 -    origShape = None
 -    if len(X.shape)==1 and X.dtype.names is None:
 -        origShape = X.shape
 -        X.shape = len(X), 1
      for row in X:
 -        fh.write(delimiter.join([fmt%val for val in row]) + '\n')
 +        fh.write(format%tuple(row) + '\n')

 -    if origShape is not None:
 -        X.shape = origShape
 -
  import re
  def fromregex(file, regexp, dtype):
      """Construct a record array from a text file, using regular-
 expressions parsing.
 }}}

-- 
Ticket URL: <http://scipy.org/scipy/numpy/ticket/663#comment:2>
NumPy <http://projects.scipy.org/scipy/numpy>
The fundamental package needed for scientific computing with Python.


More information about the Numpy-tickets mailing list