行numpy数组保存为txt文件

时间:2022-07-02 21:42:44

I have an numpy array of form

我有一个numpy数组

a = [1,2,3]

which I want to save to a .txt file such that the file looks like:

我想保存到.txt文件,使文件看起来像:

1 2 3

If I use numpy.savetxt then I get a file like:

如果我使用numpy.savetxt然后我得到一个文件,如:

1
2
3

There should be a easy solution to this I suppose, any suggestions?

我想应该有一个简单的解决方案,任何建议?

6 个解决方案

#1


32  

If numpy >= 1.5, you can do:

如果numpy> = 1.5,你可以这样做:

# note that the filename is enclosed with double quotes,
# example "filename.txt"

#注意文件名用双引号括起来,#example“filename.txt”

numpy.savetxt("filename", a, newline=" ")

Edit

several 1D arrays with same length

几个相同长度的1D阵列

a = numpy.array([1,2,3])
b = numpy.array([4,5,6])
numpy.savetxt(filename, (a,b), fmt="%d")

# gives:
# 1 2 3
# 4 5 6

several 1D arrays with variable length

几个可变长度的1D阵列

a = numpy.array([1,2,3])
b = numpy.array([4,5])

with open(filename,"w") as f:
    f.write("\n".join(" ".join(map(str, x)) for x in (a,b)))

# gives:
# 1 2 3
# 4 5

#2


14  

An alternative answer is to reshape the array so that it has dimensions (1, N) like so:

另一个答案是重新整形数组,使其具有尺寸(1,N),如下所示:

savetext(filename, a.reshape(1, a.shape[0]))

#3


6  

import numpy
a = numpy.array([1,2,3])

with open(r'test.txt', 'w') as f:
    f.write(" ".join(map(str, a)))

#4


2  

I found that the first solution in the accepted answer to be problematic for cases where the newline character is still required. The easiest solution to the problem was doing this:

我发现接受的答案中的第一个解决方案对于仍然需要换行符的情况是有问题的。解决这个问题的最简单方法是:

numpy.savetxt(filename, [a], delimiter='\t')

#5


1  

import numpy as np

a = [1,2,3]    
b = np.array(a).reshape((1,3))    
np.savetxt('a.txt',b,fmt='%d')

#6


-1  

just

' '.join(a)

and write this output to a file.

并将此输出写入文件。

#1


32  

If numpy >= 1.5, you can do:

如果numpy> = 1.5,你可以这样做:

# note that the filename is enclosed with double quotes,
# example "filename.txt"

#注意文件名用双引号括起来,#example“filename.txt”

numpy.savetxt("filename", a, newline=" ")

Edit

several 1D arrays with same length

几个相同长度的1D阵列

a = numpy.array([1,2,3])
b = numpy.array([4,5,6])
numpy.savetxt(filename, (a,b), fmt="%d")

# gives:
# 1 2 3
# 4 5 6

several 1D arrays with variable length

几个可变长度的1D阵列

a = numpy.array([1,2,3])
b = numpy.array([4,5])

with open(filename,"w") as f:
    f.write("\n".join(" ".join(map(str, x)) for x in (a,b)))

# gives:
# 1 2 3
# 4 5

#2


14  

An alternative answer is to reshape the array so that it has dimensions (1, N) like so:

另一个答案是重新整形数组,使其具有尺寸(1,N),如下所示:

savetext(filename, a.reshape(1, a.shape[0]))

#3


6  

import numpy
a = numpy.array([1,2,3])

with open(r'test.txt', 'w') as f:
    f.write(" ".join(map(str, a)))

#4


2  

I found that the first solution in the accepted answer to be problematic for cases where the newline character is still required. The easiest solution to the problem was doing this:

我发现接受的答案中的第一个解决方案对于仍然需要换行符的情况是有问题的。解决这个问题的最简单方法是:

numpy.savetxt(filename, [a], delimiter='\t')

#5


1  

import numpy as np

a = [1,2,3]    
b = np.array(a).reshape((1,3))    
np.savetxt('a.txt',b,fmt='%d')

#6


-1  

just

' '.join(a)

and write this output to a file.

并将此输出写入文件。