使用 table 编写适合文件时如何停止打印数据类型?

How to stop astropy printing dtypes when writing a fits file with a table?

我有这个测试代码 table.py :

import numpy as np
from astropy.io import fits
counts = np.array([312, 334, 308, 317])
names = np.array(['NGC1', 'NGC2', 'NGC3', 'NGC4'])
c1 = fits.Column(name='target', format='10A', array=names)
c2 = fits.Column(name='counts', format='J', unit='DN', array=counts)
coldefs = fits.ColDefs([c1, c2])
tblHdu  = fits.TableHDU.from_columns(coldefs)
hdu     = fits.PrimaryHDU(np.random.randn(1000))
hduList = fits.HDUList([hdu])
hduList.append ( tblHdu )
hduList.writeto ( "test.fits", output_verify="ignore", overwrite=True )

并且它会产生如下不需要的输出:

python3 table.py 
A10
I11

我想删除那些 "A10" 和 "I11" 打印输出,我该怎么做?

您可以在写出 FITS 文件时暂时抑制标准系统输出。以下代码段将标准输出重定向到 /dev/null,保存 test.fits,然后将输出恢复到 sys.stdout

import os
import sys 
with open(os.devnull, "w") as stdout_null:
    stdout_sys = sys.stdout
    sys.stdout = stdout_null
    hduList.writeto ("test.fits", output_verify="ignore", overwrite=True)
    sys.stdout = stdout_sys

升级astropy。

2016 年 11 月 4 日在提交 aaaa6fb, which was only removed a year later on Nov 8, 2017, in commit 68050d8 中意外引入了一个 print() 函数。

版本 2.0.4 和 3.0(当前)在 2017 年 11 月 8 日之后发布,不再有烦人的 print() 输出。