def savetxt(fname, X, fmt='<STR_LIT>', delimiter='<STR_LIT:U+0020>', newline='<STR_LIT:\n>', header='<STR_LIT>',<EOL>footer='<STR_LIT>', comments='<STR_LIT>'):
<EOL>if isinstance(fmt, bytes):<EOL><INDENT>fmt = asstr(fmt)<EOL><DEDENT>delimiter = asstr(delimiter)<EOL>own_fh = False<EOL>if _is_string_like(fname):<EOL><INDENT>own_fh = True<EOL>if fname.endswith('<STR_LIT>'):<EOL><INDENT>import gzip<EOL>fh = gzip.open(fname, '<STR_LIT:wb>')<EOL><DEDENT>else:<EOL><INDENT>if sys.version_info[<NUM_LIT:0>] >= <NUM_LIT:3>:<EOL><INDENT>fh = open(fname, '<STR_LIT:wb>')<EOL><DEDENT>else:<EOL><INDENT>fh = open(fname, '<STR_LIT:w>')<EOL><DEDENT><DEDENT><DEDENT>elif hasattr(fname, '<STR_LIT>'):<EOL><INDENT>fh = fname<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>try:<EOL><INDENT>X = np.asarray(X)<EOL>if X.ndim == <NUM_LIT:1>:<EOL><INDENT>if X.dtype.names is None:<EOL><INDENT>X = np.atleast_2d(X).T<EOL>ncol = <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>ncol = len(X.dtype.descr)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>ncol = X.shape[<NUM_LIT:1>]<EOL><DEDENT>iscomplex_X = np.iscomplexobj(X)<EOL>if type(fmt) in (list, tuple):<EOL><INDENT>if len(fmt) != ncol:<EOL><INDENT>raise AttributeError('<STR_LIT>' % str(fmt))<EOL><DEDENT>format = asstr(delimiter).join(map(asstr, fmt))<EOL><DEDENT>elif isinstance(fmt, str):<EOL><INDENT>n_fmt_chars = fmt.count('<STR_LIT:%>')<EOL>error = ValueError('<STR_LIT>' % fmt)<EOL>if n_fmt_chars == <NUM_LIT:1>:<EOL><INDENT>if iscomplex_X:<EOL><INDENT>fmt = ['<STR_LIT>' % (fmt, fmt), ] * ncol<EOL><DEDENT>else:<EOL><INDENT>fmt = [fmt, ] * ncol<EOL><DEDENT>format = delimiter.join(fmt)<EOL><DEDENT>elif iscomplex_X and n_fmt_chars != (<NUM_LIT:2> * ncol):<EOL><INDENT>raise error<EOL><DEDENT>elif ((not iscomplex_X) and n_fmt_chars != ncol):<EOL><INDENT>raise error<EOL><DEDENT>else:<EOL><INDENT>format = fmt<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>' % (fmt,))<EOL><DEDENT>if len(header) > <NUM_LIT:0>:<EOL><INDENT>header = header.replace('<STR_LIT:\n>', '<STR_LIT:\n>' + comments)<EOL>fh.write(asbytes(comments + header + newline))<EOL><DEDENT>if iscomplex_X:<EOL><INDENT>for row in X:<EOL><INDENT>row2 = []<EOL>for number in row:<EOL><INDENT>row2.append(number.real)<EOL>row2.append(number.imag)<EOL><DEDENT>fh.write(asbytes(format % tuple(row2) + newline))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>for row in X:<EOL><INDENT>fh.write(asbytes(format % tuple(row) + newline))<EOL><DEDENT><DEDENT>if len(footer) > <NUM_LIT:0>:<EOL><INDENT>footer = footer.replace('<STR_LIT:\n>', '<STR_LIT:\n>' + comments)<EOL>fh.write(asbytes(comments + footer + newline))<EOL><DEDENT><DEDENT>finally:<EOL><INDENT>if own_fh:<EOL><INDENT>fh.close()<EOL><DEDENT><DEDENT>
Save an array to a text file.

Parameters
----------
fname : filename or file handle
    If the filename ends in ``.gz``, the file is automatically saved in
    compressed gzip format.  `loadtxt` understands gzipped files
    transparently.
X : array_like
    Data to be saved to a text file.
fmt : str or sequence of strs, optional
    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. For complex `X`, the legal options
    for `fmt` are:
        a) a single specifier, `fmt='%.4e'`, resulting in numbers formatted
            like `' (%s+%sj)' % (fmt, fmt)`
        b) a full string specifying every real and imaginary part, e.g.
            `' %.4e %+.4j %.4e %+.4j %.4e %+.4j'` for 3 columns
        c) a list of specifiers, one per column - in this case, the real
            and imaginary part must have separate specifiers,
            e.g. `['%.3e + %.3ej', '(%.15e%+.15ej)']` for 2 columns
delimiter : str, optional
    String or character separating columns.
newline : str, optional
    String or character separating lines.

    .. versionadded:: 1.5.0
header : str, optional
    String that will be written at the beginning of the file.

    .. versionadded:: 1.7.0
footer : str, optional
    String that will be written at the end of the file.

    .. versionadded:: 1.7.0
comments : str, optional
    String that will be prepended to the ``header`` and ``footer`` strings,
    to mark them as comments. Default: '# ',  as expected by e.g.
    ``numpy.loadtxt``.

    .. versionadded:: 1.7.0


See Also
--------
save : Save an array to a binary file in NumPy ``.npy`` format
savez : Save several arrays into an uncompressed ``.npz`` archive
savez_compressed : Save several arrays into a compressed ``.npz`` archive

Notes
-----
Further explanation of the `fmt` parameter
(``%[flag]width[.precision]specifier``):

flags:
    ``-`` : left justify

    ``+`` : Forces to precede 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
    if it has more characters.

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 explanation of ``fmt`` is not complete, for an exhaustive
specification see [1]_.

References
----------
.. [1] `Format Specification Mini-Language
       <http://docs.python.org/library/string.html#
       format-specification-mini-language>`_, Python Documentation.

Examples
--------
>>> x = y = z = np.arange(0.0,5.0,1.0)
>>> np.savetxt('test.out', x, delimiter=',')   # X is an array
>>> np.savetxt('test.out', (x,y,z))   # x,y,z equal sized 1D arrays
>>> np.savetxt('test.out', x, fmt='%1.4e')   # use exponential notation