使用numpy连接两列

时间:2014-02-18 11:51:17

标签: python numpy

我有两个文本文件ra.txtdec.txt,每个文件包含一列。我想在第三个文件中将这两列连接到一列,如下所示:

ra.txt

115.07433  
114.75551
114.85937
111.18574
192.78617
189.71056
0.47592
2.22369
2.97497
5.44140
0.29367
308.83178

dec.txt

-88.84627
-88.68575
-88.68499
-88.63843
-88.70691
-88.69978
-87.12709
-87.12417
-87.11521
-86.95877
-86.94902
-87.04636

我希望输出文件为:

115.07433-88.84627
114.75551-88.68575
114.85937-88.68499
111.18574-88.63843
192.78617-88.70691
189.71056-88.69978
0.47592-87.12709
2.22369-87.12417
2.97497-87.11521
5.44140-86.95877
0.29367-86.94902
308.83178-87.04636

你能帮帮我吗?

1 个答案:

答案 0 :(得分:3)

您可以使用numpy.loadtxtnumpy.column_stacknumpy.savetxt

>>> import numpy as np
>>> a1 = np.loadtxt('ra.txt')
>>> a2 = np.loadtxt('dec.txt')
>>> np.savetxt('out.txt', np.column_stack((a1, a2)), delimiter=' ', fmt='%.5f')
>>> !cat out.txt
115.07433 -88.84627
114.75551 -88.68575
114.85937 -88.68499
111.18574 -88.63843
192.78617 -88.70691
189.71056 -88.69978
0.47592 -87.12709
2.22369 -87.12417
2.97497 -87.11521
5.44140 -86.95877
0.29367 -86.94902
308.83178 -87.04636