Bra*_*Lee 5 python arrays string numpy writefile
我有一个大小为(flatten3、3、19、19)的数组,我将其用于获取大小为3249的数组。
我不得不将这些值与其他一些数据一起写入文件,因此我做了以下操作以将数组转换为字符串。
np.array2string(arr.flatten(), separator=', ', suppress_small=False)
但是,当我写完文件后检查文件的内容时,我注意到我,... ,在数组中间有如下内容
[ 0.09720755, -0.1221265 , 0.08671697, ..., 0.01460444, 0.02018792,
0.11455765]
如何获取包含所有元素的数组字符串,以便潜在地将所有数据保存到文件中?
据我了解array2string,它只是为了返回数组的“漂亮”字符串表示形式。
numpy.ndarray.tofile可能是适合您目的的更好选择 - https://docs.scipy.org/doc/numpy/reference/ generated/numpy.ndarray.tofile.html 。它应该将数组的完整内容写入给定文件。
with open("test.bin", "wb") as f:
arr.flatten().tofile(f)
Run Code Online (Sandbox Code Playgroud)
您当然可以使用numpy.fromfile- https://docs.scipy.org/doc/numpy/reference/ generated/numpy.fromfile.html 读回它。
with open("test.bin", "rb") as f:
arr = numpy.fromfile(f)
Run Code Online (Sandbox Code Playgroud)