user
user

Reputation: 914

numpy savetxt is not adding comma delimiter

numpy savetxt is not adding comma delimiter

I have an array with the following contents:

3.880631596916139792e-01
6.835074831218364011e-01
4.604322858429276133e-01
3.494236368132551673e-01
7.142120448019100287e-01
2.579415438181463793e-01
8.230159985476581674e-01
7.342531681855216652e-01
3.196536650498674748e-01
7.444435819161493439e-01

And I save it as follows:

 np.savetxt('x.train.1.txt',XTraining, delimiter=',') 

However when I look into the txt file, there is no commas.

Upvotes: 12

Views: 10760

Answers (3)

robm
robm

Reputation: 1051

You can specify a format, e.g. fmt='%d,', with the comma(s) in it as you want in the output.

np.savetxt('x.train.1.txt',XTraining, fmt='%d,') 

Also works with multiple columns

np.savetxt('x.train.1.txt',XTraining, fmt='%s, %f, %f,')

Upvotes: 1

hansaplast
hansaplast

Reputation: 11573

I guess the default use case is to store a list of lists, that's why you either need to treat it as list of only one list:

np.savetxt('x.train.1.txt',[XTraining], delimiter=',')

Or put the comma instead of the newlines (note: this adds a trailing comma)

np.savetxt('x.train.1.txt',XTraining, newline=',')

Upvotes: 17

Paul Panzer
Paul Panzer

Reputation: 53029

If you want them on separate lines and comma separated

np.savetxt('x.train.1.txt', XTraining[None, :], delimiter=',\n')

Upvotes: 3

Related Questions