gpt4 book ai didi

python - np.savetxt 输出 float 据需要哪个参数?

转载 作者:太空宇宙 更新时间:2023-11-03 17:38:09 24 4
gpt4 key购买 nike

我有一小段运行良好的代码,但是我正在努力确定将其输出到 .txt 文件的方法。这是代码:

with open("Coord") as f:
line=f.readline()
for line in f:
coords=map(float,line.split(" "))
if poly.contains(Point(coords[0],coords[1])):
print line

打印命令可以工作并在终端中显示我需要的内容,但是我还无法找到保存它的方法。这是我到目前为止所尝试过的:

np.savetxt('inside.txt', np.vstack((line.str())).T)

AttributeError: 'str' object has no attribute 'str'
<小时/>
np.savetxt('inside.txt', line)

IndexError: tuple index out of range
<小时/>
np.savetxt('inside.txt', np.transpose([line])

TypeError: float argument required, not numpy.string_
<小时/>
np.savetxt('inside.txt', line, delimiter=" ", fmt="%s")

IndexError: tuple index out of range
<小时/>

总的来说,我对 python 和代码仍然相当缺乏经验,希望有人能够解释此处使用的正确格式。提前致谢。

最佳答案

来自documentation ,您可以清楚地看到 np.savetext 需要一个 array_like 对象作为第二个参数。

您可以在保存之前尝试将 line 转换为 array ,例如 -

np.savetxt('inside.txt', np.array(line.split(" ")), delimiter=" ", fmt="%s")

关于python - np.savetxt 输出 float 据需要哪个参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30930481/

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