python中的KeyError

时间:2012-06-20 13:24:45

标签: python

这是我试图在python中运行的程序的一部分,但是它在行上给了我KeyError

def xi_group_prep(infile=os.environ ['GAMA_DATA'] + '/groups/G3Cv04/G3CFoFGroup194v04.dat'

代码:

def xi_mass_samples(infile='/export/scratch/loveday/gama/stellarMasses/StellarMassesPlus.fits',
                    ranfile='/export/scratch/loveday/gama/mask/mass_randoms_194.dat',):
    """
    Create mass sub-samples for xi(s).
    """
    Mlimits = (5, 9.5, 10, 10.5, 11, 11.5, 15)
    for i in range(len(Mlimits)-1):
        Mlo = Mlimits[i]
        Mhi = Mlimits[i+1]
        galout = 'gal_lgm' + str(Mlo) + '_' + str(Mhi) + '.dat'
        ranout = 'ran_lgm' + str(Mlo) + '_' + str(Mhi) + '.dat'
        xi_select(infile,
                  ranfile=ranfile,
                  galout=galout, ranout=ranout,
                  zrange=(0.002, 0.5), nz=50, 
                  appMin=14, appMax=19.4, lgmMin=Mlo, lgmMax=Mhi)

def xi_group_prep(infile=os.environ ['GAMA_DATA'] + '/groups/G3Cv04/G3CFoFGroup194v04.dat',
                  ranfile='/export/scratch/loveday/gama/mask/mass_randoms_194.dat',
                  galout='group.dat', ranout = 'ran_group.dat',
                  zrange=(0.002, 0.5), lgmMin=5, lgmMax=15,
                  appMin=14, appMax=19.4, absMin=-30, absMax=12):

1 个答案:

答案 0 :(得分:1)

正如错误消息可能显示的那样,os.environ没有密钥'GAMA_DATA',因此在尝试访问密钥时会提供KeyError。请注意,默认参数是在函数定义时计算的,因此此键必须在此时出现。