例一:
np.savetxt('txtdata', data,fmt = ['%f']*data.shape[1],newline = '\n')`
这里data.shape[1]指的是data这个数组的列数,意思是将data数组的每一列都按照%f的格式写入
例二:
np.savetxt(FILENAME, a, fmt="%d", delimiter=",")
参数 delimiter 可以指定各种分隔符、针对特定列的转换器函数、需要跳过的行数等。
例三:
import numpy as np
a=np.arange(0,10,0.5).reshape(4,-1)
np.savetxt("out.txt",a,fmt="%d",delimiter=",") # 改为保存为整数,以逗号分隔
b = np.loadtxt("out.txt",delimiter=",") # load 时也要指定为逗号分隔
print(b)
输出结果为:
[[0. 0. 1. 1. 2.]
[2. 3. 3. 4. 4.]
[5. 5. 6. 6. 7.]
[7. 8. 8. 9. 9.]]
例四:
`np.savetxt('txtdata', data,fmt = '%d %f %f %f %f %f %f %f %f %f',newline = '\n')`
一系列格式或多格式字符串,如“ %d %10.5f”,在这种情况下忽略分隔符,不同的列对应不同的规则。
其中例子4最终解决了我的问题。