繁体   English   中英

如何将两个数组写入 python 中的“.dat”文件?

[英]How do I write two arrays to a ".dat"file within python?

我需要在列中并排打印两个数组到“.dat”文件。 这是我的代码:

rc=[1,2,3]
vc=[3,2,1]
dat = np.array([rc, vc])
a= np.column_stack((dat))
e= "There are 3 elements in this array"
hdrtxt='# rc in AU','#vc in km/s'
np.savetxt('jb_vfreefall.dat', a, delimiter=',',header=e,hdrtxt )

我希望让文件看起来像这样:

“这个数组中有 3 个元素”

#rc in AU    #vc in km/s

  1               3

  2               2

  3               1

您不能同时将e作为标题和hdrtxt作为标题,请选择一个。

如果你想使用hdrtxt你应该修改它:

hdrtxt='# rc in AU','#vc in km/s'

到:

hdrtxt='# rc in AU, #vc in km/s'

当我打电话给:

hdrtxt='# rc in AU, #vc in km/s'
np.savetxt('jb_vfreefall.dat', a, delimiter=',', header=hdrtxt)

该文件如下所示:

# # rc in AU, #vc in km/s
1.000000000000000000e+00,3.000000000000000000e+00
2.000000000000000000e+00,2.000000000000000000e+00
3.000000000000000000e+00,1.000000000000000000e+00

阅读更多关于numpy.savetxt

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM