Skip to content

savetxt ticket #1236 #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
12 changes: 11 additions & 1 deletion numpy/lib/npyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,8 @@ def split_line(line):
return X


def savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n'):
def savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='',
footer='', commentstr=''):
"""
Save an array to a text file.

Expand All @@ -853,6 +854,13 @@ def savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n'):
Character separating columns.
newline : str
.. versionadded:: 1.5.0
header : str
String that will be written to the beginning of the file. (Remember to
add a newline, ``'\n'``, at the end of the line, if desired.)
footer : str
String that will be written to the end of the file.
commentstr : str
String that will be prepended to the ``header`` and ``footer`` strings.

Character separating lines.

Expand Down Expand Up @@ -976,8 +984,10 @@ def savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n'):
else:
format = fmt

fh.write(asbytes(commentstr + header))
for row in X:
fh.write(asbytes(format % tuple(row) + newline))
fh.write(asbytes(commentstr + footer))
finally:
if own_fh:
fh.close()
Expand Down