我有这个测试代码称为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文件时,可以暂时取消标准系统输出。以下代码段将stdout重定向到/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

关于python - 用表格编写fits文件时如何停止星形打印dtypes?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48807335/

10-14 17:42