【发布时间】:2019-03-22 16:09:00
【问题描述】:
我使用 loadtxt 来初始化数组。
source = np.loadtxt('source.txt').astype(int)
之后我在函数中使用了这个数组,它的主体是:
file = open('johnson.txt', 'ab')
first = increase(np.argsort(source[0]))
np.savetxt(file, first, delimiter='-', fmt='%i')
file.close()
因此,在 txt 文件中我应该有这个:
7-1-3-6-2-4-8-5
但我有这个:
7
1
3
6
2
4
8
5
我必须以二进制模式打开文件,因为我需要将另一行附加到文件中。那么,我该如何解决呢? 谢谢!
【问题讨论】:
-
试试
np.savetxt(file, np.atleast_2d(first), delimiter='-', fmt='%i')或np.savetxt(file, first.reshape(-1, 1), delimiter='-', fmt='%i') -
np.atleast_2d(first)工作完美!谢谢!