-1

我正在编写一段代码,我需要同时将数据写入两个单独的数据文件。但是这似乎不可能,因为当我关闭一个时,它们都关闭了。可能有一种更简单的方法可以保存我的数据并将其从括号中剥离,而无需将其写入文件,但我不知道。这是我的代码:

datafile=open('new.txt', 'w')
extension = np.arange(1,3)
for ext in extension:
        if ext < 10:
            ext = '0'+str(ext)
        else:
            ext = str(ext)
        starfile=drcat+'DECam_00187567_'+ext+'_star_catalog.fits'
        #print starfile


    xc = pf.getdata(starfile).XWIN_IMAGE
    yc = pf.getdata(starfile).YWIN_IMAGE
    rmag = pf.getdata(starfile).MAG_AUTO
    stamp=getStamp(data=data,xcoord=xc,ycoord=yc,Npix =25)
    zz=len(stamp)
    print "stamp length=", zz

        kool=np.arange(0,zz)
    print kool
    datafile=open(str(ext)+'temp.txt', 'w')
    for lol in kool:
        b=stamp[lol]
        name=drstamp+str(ext)+'_'+str(lol)+'_stamp.fits' #write every stamp to a file
        sigma=1.1/0.27
            a=complexMoments(b, sigma)
        datafile.write(str(a)+'\n') # I want this to write to str(ext)+'temp.txt'
    datafile.close()

    """find the mean ellipicity for one image"""
    com=['cat ', str(ext)+"temp.txt " """| tr -d "s/,()*//g"> """ +str(ext)+"_temp.txt"]
    s0=''
    com=s0.join(com)
    print com
    res=os.system(com) #this stips the code of its brackets and commas, is there an easier way of doing this?


    ellipicity=np.genfromtxt(str(ext)+'_temp.txt').T[0]
    print ellipicity
    meanellip=np.mean(ellipicity)
    print "mean ellipicity=",meanellip
    print "\n"
    datafile.write(str(meanellip)+'\n')## I want this to write to new.txt
datafile.close()

所以基本上在我的代码结束时,我需要将meanellip写入数据文件(new.txt),但是如果我在循环内打开数据文件,它每次都会打开它并重写它自己。有没有办法让每个 datafile.write 对应于某个数据文件?如果没有,我可以使用更好的方法来实现所有数据的均值椭圆。

4

1 回答 1

3

一个对象的一个​​变量(在这种情况下是一个文件)。下次再考虑吧:)

于 2013-06-12T14:51:33.833 回答