将numpy数组的元组保存到磁盘吗? [英] Save tuple of numpy arrays to disk?

查看:110
本文介绍了将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.

    这篇关于将numpy数组的元组保存到磁盘吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆