我差不多已经完成了这个程序,但是我无法得到我想要的输出。这是代码:
import numpy as np
filename = (raw_input("Which file are we loading? "))
header = input("How many header lines? ")
# Input variables for column numbers and limiting factors
pcol = input("What column number is parallax in? ")
vcol = input("What column number is Vmag (or other limiting mag) in? ")
pmcol = input("What column number does proper motion begin in (Mu/PA or MuRa MuDec format) ")
ra = input("What column number is RA1 in? (h m s format) ")
dec = input("What column number is Dec1 in? (d m s format) ")
maglim = input("What is your limiting (dimmest) magnitude? ")
parlim = input("What is your limiting (farthest) parallax? ")
# Read in entire file
data = np.loadtxt(filename,skiprows=header,dtype=str)
# Select correct columns
parallax_str = data[:,pcol-1]
ra1_str = data[:,(ra-1)]
ra2_str = data[:,(ra)]
ra3_str = data[:,(ra+1)]
dec1_str = data[:,(dec-1)]
dec2_str = data[:,(dec)]
dec3_str = data[:,(dec+1)]
vmag_str = data[:,vcol-1]
pm1_str = data[:,(pmcol-1)]
pm2_str = data[:,(pmcol)]
# Convert selected strings to floats
parallax = parallax_str.astype(float)
vmag = vmag_str.astype(float)
pm1 = pm1_str.astype(float)
pm2 = pm2_str.astype(float)
criteria = (parallax>=parlim) & (vmag<=maglim)
vmag = vmag[criteria]
parallax = parallax[criteria]
pm1 = pm1[criteria]
pm2 = pm2[criteria]
ra1_str = ra1_str[criteria]
ra2_str = ra2_str[criteria]
ra3_str = ra3_str[criteria]
dec1_str = dec1_str[criteria]
dec2_str = dec2_str[criteria]
dec3_str = dec3_str[criteria]
newlist = np.vstack((ra1_str,ra2_str,ra3_str,dec1_str,dec2_str,dec3_str,vmag,parallax,pm1,pm2))
newlist = newlist.T
np.savetxt('observe.list',newlist,fmt='%s')
现在,这给了我一个看起来像这样的文件:
00 19 05.563 -09 57 53.47 9.92 47.43 -35.62 -301.99
00 24 25.933 -27 01 36.38 7.91 54.87 665.64 83.67
00 39 21.806 +21 15 01.71 5.87 90.42 -461.32 -370.02
00 45 04.894 +01 47 07.88 8.01 46.37 -49.08 -573.23
00 48 22.977 +05 16 50.21 5.72 134.14 757.11 -1141.33
00 51 21.754 +18 44 21.31 9.21 46.79 54.1 -267.14
但是,我想要一个看起来像这样的文件:
00 19 05.563 -09 57 53.47 9.92 47.43 -35.62 -301.99
00 24 25.933 -27 01 36.38 7.91 54.87 665.64 83.67
00 39 21.806 +21 15 01.71 5.87 90.42 -461.32 -370.02
00 45 04.894 +01 47 07.88 8.01 46.37 -49.08 -573.23
00 48 22.977 +05 16 50.21 5.72 134.14 757.11 -1141.33
00 51 21.754 +18 44 21.31 9.21 46.79 54.10 -267.14
这样一切都是正确对齐的。任何人都可以帮我找出一种有效的方法吗?
干杯。
答案 0 :(得分:2)
您可以通过将序列传递给&#39; fmt&#39;来为每列制作不同的格式。关键字参数。以下三个示例显示了不同的选项更多信息,请访问docs.scipy.org。
>>> import numpy as np
>>> a = np.random.random((3,4))
>>> a[1] = a[1] + 10
>>> a
array([[ 0.66860114, 0.29021582, 0.47168711, 0.86839242],
[ 10.41030497, 10.22771623, 10.80389801, 10.6170771 ],
[ 0.47201727, 0.90861352, 0.03952651, 0.67245859]])
>>>
>>> # saved using string format
>>>
>>> np.savetxt('string.txt', a, fmt='%s')
>>> with open('string.txt','r') as f: print(f.read())
...
0.668601144977 0.290215822112 0.471687110847 0.86839242197
10.4103049716 10.2277162318 10.8038980106 10.617077099
0.472017270547 0.9086135154 0.0395265080276 0.672458588797
>>>
>>> # saved using floating format with the "width" parameter = 10
>>>
>>> np.savetxt('fixed.txt',a, fmt='%10.4f')
>>> with open('fixed.txt','r') as f: print(f.read())
...
0.6686 0.2902 0.4717 0.8684
10.4103 10.2277 10.8039 10.6171
0.4720 0.9086 0.0395 0.6725
>>>
>>> # saved using format specifier sequence with format for each column
>>>
>>> np.savetxt('multi.txt',a, fmt=('%5.2f', '%10.4f', '%7.3f', '%12.1f'))
>>> with open('multi.txt','r') as f: print(f.read())
...
0.67 0.2902 0.472 0.9
10.41 10.2277 10.804 10.6
0.47 0.9086 0.040 0.7
答案 1 :(得分:0)
您想要达到的目标是所谓的“固定宽度”&#39; CSV文件。由于numpy和pandas都不能将数据导出到这样格式的csv文件,如果你知道你必须处理的数字位数,你可以使用numpy.savetext()
的格式说明符来解决这个问题: / p>
#!/usr/bin/env python3
# coding: utf-8
import numpy as np
# generate a random array to get some sample data
newlist = np.random.rand(3,2)
# assume you have three decimal digits and seven in total (so four in front of the decimal sign)
np.savetxt('observe.list', newlist, fmt='%7.3f')
,并提供:
0.021 0.571
0.471 0.416
0.177 0.720
由于评论而更新:
newlist = np.array([1.5, 10.55555, 0.3, 2, 5.0])
np.savetxt('observe.list', newlist, fmt='%10.5f')
,并提供:
1.50000
10.55555
0.30000
2.00000
5.00000