gpt4 book ai didi

python - 一个简单的命令 - "np.save",也许我误解了

转载 作者:行者123 更新时间:2023-12-01 04:39:55 25 4
gpt4 key购买 nike

刚刚完成 numpy.save 的示例 -- http://docs.scipy.org/doc/numpy/reference/generated/numpy.save.html

示例

from tempfile import TemporaryFile

outfile = TemporaryFile()

x = np.arange(10)

np.save(outfile, x)

执行此命令(突出显示)后,为什么我在当前目录中找不到名为“outfile”的输出文件?抱歉,这听起来可能很愚蠢

outfile.seek(0) # Only needed here to simulate closing & reopening file

np.load(outfile)

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

最佳答案

这是因为您使用的是 - TemporaryFile ,它是一个临时文件,不会作为 outfile 存储在您的目录中。

如果您想将其保存到 outfile ,您可以将其作为名称,它会将其保存到该文件中。

np.save('outfile',x)

当像这样保存以加载时,您将需要再次使用文件名作为字符串 -

np.load('outfile')

关于python - 一个简单的命令 - "np.save",也许我误解了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30951227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com