Skip to main content
 首页 » 编程设计

python之循环并发内部循环

2024年10月17日8webabcd

我有一个从 txt 文件调用信息的程序。文本文件中的每一行对应于不同目录中的整个文件。我想从 txt 文件(第 1 行)中读取信息,并将其写入输出文件,其中包含目录(文件 1)中相应文件的信息。我需要我的代码来处理多个文件。如何让每个循环相互对应以产生正确的输出?

import datetime 
import glob 
 
f = open('\\_spec_final.t15', 'w') 
infofile = open('info.txt', 'rt') 
num_lines = sum(1 for line in 'info.txt') 
count = 0 
while count <= num_lines 
    for line in infofile:  
        lat = float(line[88:94])  
        lon = float(line[119:127]) 
        year = int(line[190:194]) 
        month = int(line[195:197]) 
        day = int(line[198:200]) 
        hour = int(line[201:203]) 
        minute = int(line[204:206]) 
        second = int(line[207:209]) 
        dur = float(line[302:315]) 
        numpoints = float(line[655:660]) 
        fov = line[481:497] # field of view? 
        sza = float(line[418:426]) 
        snr = 0.0000  
        roe = 6396.2  
        res = 0.5000 
        lowwav = float(lowwav) 
        highwav = float(highwav) 
        spacebw = (highwav - lowwav)/ numpoints 
 
        d = datetime.datetime(year, month, day, hour, minute, second) 
        f.write('{:>12.5f}{:>12.5f}{:>12.5f}{:>12.5f}{:>8.1f}'.format(sza,roe,lat,lon,snr)) # line 1 
        f.write("\n") 
        f.write('{:>10d}{:>5d}{:>5d}{:>5d}{:>5d}{:>5d}'.format(year,month,day,hour,minute,second)) # line 2 
        f.write("\n") 
        f.write( ('{:%Y/%m/%d %H:%M:%S}'.format(d)) + "UT Solar Azimuth:" + ('{:>6.3f}'.format(sza)) + " Resolution:" + ('{:>6.4f}'.format(res)) + " Duration:" + ('{:>6.2f}'.format(dur))) # line 3 
        f.write("\n") 
        f.write('{:>21.13f}{:>26.13f}{:>24.17e}{:>12f}'.format(lowwav,highwav,spacebw,numpoints)) # line 4 
        f.write("\n") 
 
        path = '/Users/140803/*'  
        files = glob.glob(path) 
        for file(count) in files: 
            g = open(file, 'r') 
            #do stuff 
            g.close() 
 
f.close() 
infofile.close() 

请您参考如下方法:

根据对话,您似乎根本不需要内部循环。您有多个不需要存在的嵌套循环。我在此处重写了您的部分代码,但没有它们。

具体来说,如果您想遍历某些东西并同时计算它,请不要做类似的事情

for count in range(3): 
    for item in iterator: 
        print("%d %s" % (count, item)) 

这些循环的输出类似于

0 item1 
0 item2 
0 item3 
1 item1 
1 item2 
1 item3 
2 item1 
2 item2 
2 item3 

您做想要的是enumerate 函数,它在您的迭代器旁边运行一个计数器:

for count, item in enumerate(iterator): 
    print("%d %s" % (count, item)) 

这将打印

0 item1 
1 item2 
2 item3 

此外,我将所有 open() ... close() 结构替换为 with ...:

import datetime 
import glob 
 
path = '/Users/140803/*'  
files = glob.glob(path) 
 
with open('\\_spec_final.t15', 'w') as f: 
    with open('info.txt', 'rt') as infofile: 
        for count, line in enumerate(infofile):  
            lat = float(line[88:94])  
            lon = float(line[119:127]) 
            year = int(line[190:194]) 
            month = int(line[195:197]) 
            day = int(line[198:200]) 
            hour = int(line[201:203]) 
            minute = int(line[204:206]) 
            second = int(line[207:209]) 
            dur = float(line[302:315]) 
            numpoints = float(line[655:660]) 
            fov = line[481:497] # field of view? 
            sza = float(line[418:426]) 
            snr = 0.0000  
            roe = 6396.2  
            res = 0.5000 
            lowwav = float(lowwav) 
            highwav = float(highwav) 
            spacebw = (highwav - lowwav)/ numpoints 
 
            d = datetime.datetime(year, month, day, hour, minute, second) 
            f.write('{:>12.5f}{:>12.5f}{:>12.5f}{:>12.5f}{:>8.1f}'.format(sza,roe,lat,lon,snr)) # line 1 
            f.write("\n") 
            f.write('{:>10d}{:>5d}{:>5d}{:>5d}{:>5d}{:>5d}'.format(year,month,day,hour,minute,second)) # line 2 
            f.write("\n") 
            f.write( ('{:%Y/%m/%d %H:%M:%S}'.format(d)) + "UT Solar Azimuth:" + ('{:>6.3f}'.format(sza)) + " Resolution:" + ('{:>6.4f}'.format(res)) + " Duration:" + ('{:>6.2f}'.format(dur))) # line 3 
            f.write("\n") 
            f.write('{:>21.13f}{:>26.13f}{:>24.17e}{:>12f}'.format(lowwav,highwav,spacebw,numpoints)) # line 4 
            f.write("\n") 
 
            with open(files[count], 'r') as g: 
                #do stuff (to g presumably) 

虽然在 files 上使用计数器实际上可能是您想要的,但我发现这不太可能。我认为您实际上是在尝试根据刚刚加载的信息生成文件名并打开它。通配的问题在于,它甚至不能保证在同一程序的两次不同运行之间以相同的顺序返回结果。