[SciPy-dev] problem with write_array (2)

Pietro Berkes p.berkes at biologie.hu-berlin.de
Wed Oct 9 11:34:41 CDT 2002


One line has been lost during cut&paste, sorry.
The function is:

def write_array(fileobject, arr, separator=" ", linesep='\n',
                precision=5, suppress_small=0):
    """Write a rank-2 or less array to file represented by fileobject.

    Inputs:

      fileobject -- An open file object or a string to a valid filename.
      arr -- The array to write.
      separator -- separator to write between elements of the array.
      linesep -- separator to write between rows of array
      precision -- number of digits after the decimal place to write.
      suppress_small -- non-zero to suppress small digits and not write
                        them in scientific notation.

    Outputs:

      file -- The open file.
    """
      
    file = get_open_file(fileobject, mode='wa')
    rank = len(arr.shape)
    
    if rank > 2:
        raise ValueError, "Can-only write up to 2-D arrays."
    
    if rank == 0:
        h = 1
        rarr = Numeric.reshape(arr, (1,1))
    elif rank == 1:
        h = arr.shape[0]
        rarr = Numeric.reshape(arr, (h,1))
    else:
        h = arr.shape[0]
        rarr = arr
        
    for k in range(h):
        astr = Numeric.array2string(rarr[k], max_line_width=sys.maxint,
                                    precision=precision,
                                    suppress_small=suppress_small,
                                    separator=' '+separator,
                                    array_output=0)
        astr = astr[1:-1]
        file.write(astr)
        file.write(linesep)
    return file



More information about the Scipy-dev mailing list