Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python numpy.savetxt header has extra character #

I am using the following to save the numpy array x with a header:

np.savetxt("foo.csv", x, delimiter=",", header="ID,AMOUNT", fmt="%i")

However, if I open "foo.cv", the file looks like:

# ID,AMOUNT
21,100
52,120
63,29
:

There is an extra # character in the beginning of the header. Why is that and is there a way to get rid of it?

like image 962
Edamame Avatar asked Mar 24 '16 22:03

Edamame


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 convert a NumPy array to a text file?

Let us see how to save a numpy array to a text file. Creating a text file using the in-built open() function and then converting the array into string and writing it into the text file using the write() function. Finally closing the file using close() function.

What is FMT in Python?

Data to be saved to a text file. 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='%.

How do I save a NumPy array to CSV?

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.


1 Answers

The header and footer text are added as comments. If you want to change the comment identifier, pass the comments option (the default is #):

np.savetxt("foo.csv", x, delimiter=",", header="ID,AMOUNT", 
           fmt="%i", comments='')

As documented here.

like image 184
Carl Groner Avatar answered Sep 28 '22 02:09

Carl Groner