擅长:python、mysql、java
<p>要重新创建形状,需要在保存文件时保存形状。</p>
<p>尝试:</p>
<pre><code>import numpy as np
import re
result=np.array([[1.,0.,0.,0.00375,-0.01072,-0.,-1000.,],
[2.,3.,4.,0.,-0.004,750.,0.],
[3.,3.,0.,0.,0.,-750.,1000.]])
with open('/tmp/test', 'w') as fout:
fout.write(u'#'+'\t'.join(str(e) for e in result.shape)+'\n')
result.tofile(fout)
with open('/tmp/test', 'rb') as f:
line=f.readline().decode('ascii')
if line.startswith('#'):
shape=tuple(map(int, re.findall(r'(\d+)', line)))
else:
raise IOError('Failed to find shape in file')
result2=np.fromfile(f)
result3=result2.reshape(shape)
print(np.array_equal(result, result2))
# False
print(np.array_equal(result, result3))
# True
</code></pre>
<p>您可以将形状以某种形式保存在order文件中,以重新创建相同的形状。但是,请确保不要忘记文件开头的数据,因为与<a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html" rel="nofollow">np.loadtxt</a>不同,以<code>#</code>开头的行仍然被视为数据。</p>