Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python savetxt header

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?

like image 976
user2590144 Avatar asked Apr 17 '14 03:04

user2590144


People also ask

What is Savetxt in Python?

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.

How do I save a txt in Numpy?

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.

What is FMT in Savetxt?

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)


1 Answers

As numpy.savetxt, comment is prepended to header.

So, try

np.savetxt('output.txt', array, header=str(dimension), comments='')
like image 71
emeth Avatar answered Oct 16 '22 08:10

emeth