4

I am simply trying to put my 11-column, single row of values into a text file that I can append new rows to. I am used to python 2.7 and I do not understand why my np.savetxt is not behaving the same as previously.

I know that this will create my text file without having any type errors:

with open("testtext.txt", "wb") as myfile:
    np.savetxt(myfile, mydata, fmt="%.5f", delimiter=' ', newline=' ')

However when I then run my code to receive my new data that I need to append, and I change it to:

with open("testtext.txt", "ab") as myfile:
    np.savetxt(myfile, mynewdata, fmt="%.5f", delimiter=' ', newline=' ')

It does not put mynewdata into a new row. I have input \n into newline, I have tried " " instead of ' ', I have tried \r\n but that was a longshot.. Please help, I feel like this should be a simple thing. Thank you.

2 Answers 2

4

Going through your question again, so, if your new array(row) not started in a new row, that should be the last line of your text file was not ended with newline. So as @hpaulj pointed out, this should work:

with open("testtext.txt", "wb") as myfile:
    f.write(b'\n')
    ...

If you are running on windows, take '\r\n' instead of '\n'.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @Will, this worked and I did have to use '\r\n'!
0

If you save a 1d array with typical delimiter and no special newline, it saves one value per row:

In [548]: with open('test.txt','wb') as f:
     ...:     np.savetxt(f, np.arange(3), fmt='%5d', delimiter=',')   
In [549]: cat test.txt
    0
    1
    2

Using '' newline, puts them on one line - but ends the file without a \n

In [550]: with open('test.txt','wb') as f:
     ...:     np.savetxt(f, np.arange(3), fmt='%5d', delimiter=',', newline='')

In [552]: cat test.txt
    0    1    2In [553]: 

The next append will continue on that line.

I could include a b'\n' write. This needs to be a bytestring nl. (np.savetxt makes liberal use of np.lib.npyio.asbytes).

In [557]: with open('test.txt','ab') as f:
     ...:     f.write(b'\n')
     ...:     np.savetxt(f, np.arange(3), fmt='%5d', delimiter=',', newline='')

In [558]: cat test.txt
    0    1    2
    0    1    2In [559]: 

Or maybe I should make a habit of adding a \n write at the end of such a savetxt.

OR, I could just make sure I write a 2d array to the file, even if I want to write just one line. Wrapping the array in a list will do the trick. Think of savetxt iterating on the 'rows' (first dimension) of the input,and writing one row at a time (formatted with fmt, delimiter, and newline).

In [559]: with open('test.txt','wb') as f:
     ...:     np.savetxt(f, [np.arange(3)], fmt='%5d', delimiter=',')

In [560]: cat test.txt
    0,    1,    2

In [561]: with open('test.txt','ab') as f:
     ...:     np.savetxt(f, [np.arange(4)], fmt='%5d', delimiter=',')

In [562]: cat test.txt
    0,    1,    2
    0,    1,    2,    3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.