3.Python科学计算与数据处理讲义
文件存取 * a.tofile(a.bin) b = np.fromfile(a.bin, dtype=np.float) # 按照float类型读入数据 b # 读入的数据是错误的 array([ 2-314, 6-314, 1-313, 1-313, 1-313, 2-313]) a.dtype # 查看a的dtype dtype(int32) b = np.fromfile(a.bin, dtype=32) # 按照int32类型读入数据 b # 数据是一维的 array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) b.shape = 3, 4 # 按照a的shape修改b的shape b # 这次终于正确了 array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) 文件存取 从上面的例子可以看出,需要在读入的时候设置正确的dtype和shape才能保证数据一致。并且tofile函数不管数组的排列顺序是C语言格式的还是Fortran语言格式的,统一使用C语言格式输出。 此外如果fromfile和tofile函数调用
原创力文档

文档评论(0)