I want to add a header to my text file when I'm writing an array to it:
np.savetxt('output.txt', array, header = str(dimension))
but it looks like by default Python adds #
in front of the header. Is there anyway I can get rid of that?
The savetxt() function is used to save an array to a text file. Syntax: numpy.savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='', footer='', comments='#', encoding=None) Version: 1.15.0. Parameter: Name.
You can save your NumPy arrays to CSV files using the savetxt() function. This function takes a filename and array as arguments and saves the array into CSV format. You must also specify the delimiter; this is the character used to separate each variable in the file, most commonly a comma.
fmtstr 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 single specifier, fmt='%. 4e', resulting in numbers formatted like ' (%s+%sj)' % (fmt, fmt)
As numpy.savetxt, comment
is prepended to header
.
So, try
np.savetxt('output.txt', array, header=str(dimension), comments='')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With