将numpy数组的元组保存到磁盘吗?

问题描述:

当我运行

I'm getting a tuple of numpy arrays as (keypoint, descriptor) when I run the compute function to extract them from an image.

是否可以将这个元组打包在一起,以便我可以将它们保存到文件中,或者将它们作为一行写到CSV中?

Is there a way to pack this tuple together so that I can save them to a file, or write them into a CSV as a row?

有几种方法可以做到这一点:

There are a few ways you can do this:

import csv
writer = csv.writer(open("file.csv","w"))
for row in array:
     writer.writerow(str(row))

根据这个问题,会有一些格式化问题.

According to this question, there might be some formatting problems with this.

如评论中所述,您可以使用 numpy.savetxt() .这可能是最好的方法:

As mentioned in the comments, you can use numpy.savetxt(). This is probably the best way:

numpy.savetxt("file.csv",array,delimiter=",")

  • 您还可以使用 pickle模块:

  • You can also use the pickle module:

    import pickle
    pickle.dump(array,open("file","w"))
    

    正如我在上面链接的文档中所述,pickle.load()不应用于加载不受信任的数据.

    As mentioned in the docs I linked above, pickle.load() should not be used to load untrusted data.