NEMM2020
NEMM2020

Reputation: 129

Using newline as a separator with numpy.savetxt not working

I am writing the following command:

 np.savetxt('File1', File1, delimiter = ',\n')

The problem is when I open File1 in NotePad (Windows), all the numerical values I calculated come out on one line. I want each data value to start on a new line.

For example, I get: 123456

  instead of:
  1
  2
  3
  4
  5 
  6

I have tried using delimiter and newline in parameters but no luck.

Note: this does work if I open WordPad but not NotePad for some reason.

Upvotes: 2

Views: 1730

Answers (2)

Harsha Biyani
Harsha Biyani

Reputation: 7268

Try:

np.savetxt('File1', File1, newline='\r\n')

Upvotes: 0

cs95
cs95

Reputation: 402543

On windows, the separator is CRLF (carriage return + line feed). You can use \r\n as a separator.

np.savetxt(..., newline='\r\n')

Alternatively, without loss in generality, import os and use os.linesep, as suggested by @SeanBreckenridge. This would be the best option in terms of portability.

import os
np.savetxt(..., newline=os.linesep)

Upvotes: 1

Related Questions