Reputation: 45
I use loadtxt to initialize array.
source = np.loadtxt('source.txt').astype(int)
After that I use this array in function, which body is:
file = open('johnson.txt', 'ab')
first = increase(np.argsort(source[0]))
np.savetxt(file, first, delimiter='-', fmt='%i')
file.close()
As a result, in txt file I should have this:
7-1-3-6-2-4-8-5
But I have this:
7
1
3
6
2
4
8
5
I have to open file in binary mode, because I need to append another rows to file. So, how can I fix that? Thank you!
Upvotes: 3
Views: 1734
Reputation: 231335
savetxt
iterates on the input array, and writes each 'row' to a new line. For the typical 2d array that would be a row. But for a 1d array that would be an element.
So change your write to saving a 2d array:
np.savetxt('test.txt', [first], delimiter=..., fmt=...)
Assuming first
is a 1d array, then np.array([first])
is 1 row 2d, first[None,:]
would also work (or a reshape
).
To append lines, open the file in 'a' append mode. 'wb'` binary doesn't help.
Upvotes: 4