这个问题可能微不足道,但我真的不知道如何修复它。假设我使用类型numpy
生成大小为np.int64
的数组(4,3),然后写为二进制文件。该数组如下所示:
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
现在,假设我的目标是只读取第二行[ 4 5 6]
。这应该使用默认的seek()函数。为此,我写了以下内容:
import numpy as np
import os
# To write
X = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
X = np.asarray(X, dtype=np.int64)
print("%s\n\n" % X)
X.astype(np.int64).tofile('test.dat')
# To read
f = open('test.dat', "rb")
n = 4
dim = 3
r = 1
f.seek((r*dim*8), os.SEEK_SET)
data = np.fromfile(f, count=(2*dim*8), dtype=np.int64).reshape(-1, dim)
print("The Reading data is:\n %s\n" % data)
但是,虽然我只想要[ 4 5 6]
:
The Reading data is:
[[ 4 5 6]
[ 7 8 9]
[10 11 12]]
在np.fromfile
中,参数count=(r*dim*8)
似乎无效!这是一个错误吗?或者我在这里做错了什么!
谢谢
答案 0 :(得分:3)
如果您只想[4 5 6]
(1个元素包含3个项目),那么您的计数应为dim
(即3个)
data = np.fromfile(f, count=dim, dtype=np.int64).reshape(-1, dim)