Skip to main content
 首页 » 编程设计

python之使用 astropy.io.ascii 格式化

2024年09月07日12JeffreyZhao

我有一个 AstroPy 数据表:

>>> data 
 
 ra           dec          redshift ... yMeanPSFMag       yMeanPSFMagErr 
 ------------ ------------ -------- ... ----------------- -------------------- 
 
 0.34011348  26.83588138     5.75 ...  19.49519920349121   0.03307399898767471 
 0.66411726  25.84304425     5.82 ...  19.45319938659668   0.04351300001144409 
 1.4680833   -0.1154999      5.85 ...  20.118600845336914  0.18635299801826477 
 2.10739     -6.43456        5.93 ...  20.439899444580078  0.16982899606227875 

我只是想以控件格式的方式输出它:

>>>  df = data['ra', 'dec'] 
>>>  ascii.write(df, 'temp.dat', overwrite=True, formats="{df['ra']:%3.1f, df['dec']:%8.3f}") 

我得到一个:

TypeError: string indices must be integers 

我查看了 this package,但看不到任何直接有用的东西。

请您参考如下方法:

我经常这样做。贴近您所写的内容:

df = data['ra', 'dec']  
ascii.write(df, 'temp.dat', overwrite=True, formats={'ra':'%3.1f','dec':'%8.3f'}) 

或者我更喜欢的方式是

aformats = ['%.1f','%.3f'] 
oformats = dict(zip(df.colnames, aformats)) 
ascii.write(df, 'temp.dat', overwrite=True, formats=oformats) 

bc 通常不需要指定总位数。