如何使用列表作为列创建 table?

How can I make a table using lists as columns?

我在 Ubuntu 20.10 的 IDLE 中使用 Python 3.8.10。
简而言之,我有几个 .fits 文件,我必须从中读取一些参数。我已经有了我的 readfits 功能:它打开文件并将我需要的值添加到列表中。现在我需要创建一个函数,将 readfits 应用于当前目录中的某些文件(不是问题),然后将它们打印在 table 中。问题是我拥有的每个列表都是 table 的列之一,所以我不知道该怎么做。我想递归地制作它,因为实际上有 104 个 .fits 文件,所以手动完成它是一件很长的事情。
这是现在的代码:

#import needed packages
import numpy as np
import matplotlib.pyplot as plt
from astropy.io import fits
import os
from tabulate import tabulate

#make empty lists where rquantities will be added
rv = []
date = []
rv_errors = []
airmass = []
BIS = []
normal_dates = []
names = []
instrument = []


#create function which reads .fits files and adds values to global lists
def readfits(filename):

    #open files and give names to data and header
    with fits.open(filename) as hdu:
        data = hdu[0].data
        head = hdu[0].header

    #Useful quantities
    
    #Contrast of CCF in %
    contrast = head['HIERARCH TNG DRS CCF CONTRAST']

    #FWHM of CCF in km/s
    fwhm_ccf = head['HIERARCH TNG DRS CCF FWHM']

    #Number of lines used
    nr_lines = head['HIERARCH TNG DRS CCF LINES']

    #Mask type
    mask = head['HIERARCH TNG DRS CCF MASK']

    #Right ascension and declination in radians
    ra = head['RA']
    dec = head['DEC']

    #Air mass
    air_mass = head['AIRMASS']

    #Mean Julian Date of observation
    jd = head['MJD-OBS']

    #Mean BJD subtracted for plotting reasons
    bjd = head['HIERARCH TNG DRS BJD'] - 2450000

    #Exposure time in s
    exp_time = head['EXPTIME']

    #Estimated RV uncertainty in m/s
    err_rv = head['HIERARCH TNG DRS DVRMS']

    #Noise at order 46 where the stellar peak is
    #sn46 = head['HIERARCH TNG DRS SPE EXT SN46']

    #Half-window of the CCF
    half_wind = head['HIERARCH IA2 YABI WIDTHCCF']

    #Step of the CCF
    ccf_step = head['HIERARCH IA2 YABI STEPCCF']

    #Apparent magnitude
    mag = head['HIERARCH TNG TEL TARG MAG']

    #Th-lamp drift
    th_drift = head['HIERARCH TNG DRS DRIFT SPE RV']

    #RV of the system (YABI input)
    #rv_sys = head['HIERARCH TNG TEL TARG RADVEL']

    #BIS
    bis = head['HIERARCH TNG DRS BIS SPAN']

    #CCF noise from km/s to m/s
    ccf_noise = head['HIERARCH TNG DRS CCF NOISE']*1000

    #Measured RV for this target in this night in km/s
    rv_mis = head['HIERARCH TNG DRS CCF RVC']

    #RV in m/s
    rv_true = rv_mis*1000

    #readable date
    dates = head['DATE-OBS']

    #instrument used
    strum = head['INSTRUME']

    #name of target
    name = head['HIERARCH TNG OBS TARG NAME']

    #add quantities to the initial lists
    global rv
    rv.append(rv_true)
    global date
    date.append(bjd)
    global rv_errors
    rv_errors.append(ccf_noise)
    global airmass
    airmass.append(air_mass)
    global BIS
    BIS.append(bis)
    global normal_dates
    normal_dates.append(dates)
    global instrument
    instrument.append(strum)
    global names
    names.append(name)


#writes values from fits files to txt table
def writetable():

    #list with all files in directory
    list_all = os.listdir()
    list_data = []

    #take only files with 'bis' in the name
    for i in range(0, len(list_all)):
        if 'bis' in list_all[i]:
            list_data.append(list_all[i])

    #sort elements in alphabetical order
    list_data.sort()

    #apply readfits to all 'bis' files
    n = len(list_data)
    for i in range(0,n):
        readfits(list_data[i])
    
    global names
    global normal_dates
    global instrument
    global rv
    global rv_errors
    global date
    global BIS
    global airmass
    
    #headers for table
    headers = ['Target', 'Date of observation', 'Instrument', 'RV',
               'RV error', 'BJD', 'BIS', 'Air mass']

    
    param_table = tabulate([], headers = headers)
    
    print(param_table)

由于是我自己想出来的,所以我会把答案留在这里,以备不时之需。这比我想象的要容易得多。基本上我做了一个矩阵作为列表的列表,然后我转置它。这样,我就有了我想要的行和列。然后你只需将它与右边的 headers 一起制表即可。这是代码:

    #make matrix as list of lists and transpose it
    A = np.array([names, normal_dates, instrument, rv, rv_errors,
                       date, BIS, airmass])
    B = np.transpose(A)
    
    #headers for table
    headers = ['Target', 'Date of observation', 'Instrument', 'RV',
               'RV error', 'BJD', 'BIS', 'Air mass']

    
    param_table = tabulate(B, headers = headers)
    
    print(param_table)