Python函数不会结束

时间:2013-04-28 20:01:15

标签: python function csv dictionary

我有一个名为ctyear.csv的(县,州,年)元组的csv文件。对于每个元组,我需要年,县和州的快餐店和杂货店(num_ffnum_groc)的数量。此信息保存在以年份命名的Census数据文件中(因此cbp96co.csv'指的是1996年的数据),格式如下:

state, year, county, industry_code, number_of_establishments
'1', '1996', '55030', '30', '20'
'1', '1996', '55404', '31', '13' 
'14', '1996' , '23', '5411', '24'

快餐和杂货的行业准则 - 我有兴趣获取相关信息的行业 - 每年都有所不同。辅助函数whichcode() - 不包含在下面的代码中 - 返回与给定年份相关的行业代码。

鉴于一年,打开文件句柄rawffrawgrocfferrorsgrocerrors,以下函数会查找杂货店和快餐店的数量(由ctyear中每个(县,州,年)元组的相应行业代码)并将其写入上述CSV文件句柄。

此功能打印正确的结果;从这个意义上讲,它有效。它提取正确的数据。但是,它不会结束。当我调用它时,即使打印完最后一行,该功能也不会终止。当我在终端中调用此函数一年后,在打印函数的最后一行后,终端只会在接下来的几个小时显示一个闪烁的光标 - 它似乎没有停止。怎么可能出错?

def extractCBPdata(year, rawff, rawgroc, fferrors, grocerrors): 
   # Initialize file name, get the set of codes we're interested in extracting for this year using whichcode()
    fname = "cbp"+str(year)+"co.txt"
    info = whichcode(year)
    codename = info['codename']
    groccode = info['groccode']
    ffcode = info['ffcode']
    cbpind = {} 
   # Index data file by (state, county, year, code)
    for line in csv.DictReader(open(fname, 'rb')): 
        cbpind[(int(line['fipstate']), int(line['fipscty']), getyear(year), line[codename])] = line
   # Read a list of tuples of (state, county, year) that we want information about
    ctyear = csv.DictReader(open('ctyear.csv', 'r')) 
   # For every (state, county, year) tuple in the above file, get info from the cbpind for relevant codes
    for c in ctyear:  
        if c['year'] == getyear(year): 
            fipstate = int(c['fipstate']) 
            fipscty = int(c['fipscty']) 
            for fc in ffcode: 
               if (fipstate, fipscty, getyear(year), fc) in cbpind: 
                      line1 = cbpind[(fipstate, fipscty, getyear(year), fc)] 
                      st = line1['fipstate'] 
                      cty = line1['fipscty']
                      num_ff = line1['est'] 
                      row_titles = ['fipstate', 'fipscty', 'codename', 'code', 'year', 'num_ff']
                      row = [st, cty, codename, fc, getyear(year), num_ff] 
                      rawff.writerow(dict(zip(row_titles, row)))
               else: 
                      fferrors.writerow(dict(zip(['fipstate', 'fipscty', 'year', 'ffcode'], [fipstate, fipscty, getyear(year), fc])))
            for gc in groccode:         
                if (fipstate, fipscty, getyear(year), gc) in cbpind: 
                      line2 = cbpind[(fipstate, fipscty, getyear(year), gc)]
                      st = line2['fipstate'] 
                      cty = line2['fipscty'] 
                      num_groc = line2['est']
                      row_titles = ['fipstate', 'fipscty', 'codename', 'code', 'year', 'num_groc']
                      row = [st, cty, codename, fc, year, num_groc]
                      rawgroc.writerow(dict(zip(row_titles, row)))
               else: 
                      grocerrors.writerow(dict(zip(['fipstate', 'fipscty', 'year', 'grocode'], [fipstate, fipscty, year, gc])))
    print "Done writing for " + str(year)
    return

我使用以下行实现此功能:

g = ['num_groc', 'fipstate', 'fipscty', 'codename', 'code', 'year']
ff = ['num_ff', 'fipstate', 'fipscty', 'codename', 'code', 'year']
rawff = csv.DictWriter(open('raw_ff' + str(year) + '.csv', 'w'), fieldnames = ff)
rawgroc = csv.DictWriter(open('raw_groc' + str(year) + '.csv', 'w'), fieldnames = g) 
fferrors = csv.DictWriter(open('fferrors' + str(year) + '.csv', 'w'), fieldnames = ['fipstate', 'fipscty', 'year', 'ffcode'])
grocerrors = csv.DictWriter(open('grocerrors' + str(year) + '.csv', 'w'), fieldnames = ['fipstate', 'fipscty', 'year', 'grocode'])
grocerrors.writerow(dict(zip(['fipstate', 'fipscty', 'year', 'grocode'], ['fipstate', 'fipscty', 'year', 'grocode'])))
rawff.writerow(dict(zip(ff,ff)))
rawgroc.writerow(dict(zip(g,g)))
fferrors.writerow(dict(zip(['fipstate', 'fipscty', 'year', 'ffcode'],['fipstate', 'fipscty', 'year', 'ffcode'])))

years = ['96', '97', '98', '99', '00', '01', '02', '03', '04', '05']
for y in years: 
    l = extractCBPdata(y)           

0 个答案:

没有答案