gpt4 book ai didi

python - 特定的列宽和与 savetxt 对齐

转载 作者:太空宇宙 更新时间:2023-11-04 03:23:10 25 4
gpt4 key购买 nike

我快完成这个程序了,但是我无法得到我想要的输出。这是代码:

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

这样一切都是右对齐的。谁能帮我想出一个有效的方法来做到这一点?

干杯。

最佳答案

您可以通过将序列传递给“fmt”关键字参数来为每一列制作不同的格式。以下三个示例显示了不同的选项。更多信息请访问 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

关于python - 特定的列宽和与 savetxt 对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34010451/

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