2012-08-11 98 views
1

我正在帶一個標題行的csv文件(稱爲aggregate_file),按列進行排序,並將其重新寫入另一個csv文件(稱爲sorted_file)。我想要排序的列標題由variable_names指定。爲什麼我的csv排序功能不起作用?

def sortbyCounty(aggregate_file, sorted_file, *variable_names): 
    f = open(aggregate_file, 'r') 
    readit = csv.reader(f) 
    headers = readit.next() 
    col_indices = [] 
    for var in variable_names: 
     col_indices.append(headers.index(var)) 
    print col_indices 
    thedata = list(readit) 
    thedata.sort(key=operator.itemgetter(col_indices)) 
    fx = open(sorted_file, 'w') 
    writeit = csv.writer(fx) 
    writeit.writerow(headers) 
    writeit.writerows(thedata) 
    writeit.close() 
    return sorted_file 

接下來,我調用這個函數下面幾行:

aggregate_file = "Aggregate_test90.csv" 
sorted_file = "County_test90.csv" 
variable_names = 'CTYCODE90' 
test = sortbyCounty(aggregate_file, sorted_file, *variable_names) 

這是我的錯誤信息:

col_indices.append(headers.index(var)) 
ValueError: list.index(x): x not in list 

然而,當我printheaders名單,我可以清楚地看到我的變量是存在的:

['_STATE90', 'HEIGHT90', 'WEIGHT90', '_BMI90', 'AGE90', 'CTYCODE90', 'IYEAR90', 'SEX90', '_RFOBESE90'] 

所以我不明白爲什麼我收到這個錯誤信息。我錯過了什麼?

回答

2

variable_names應該是listtuple的字符串。作爲字符串也表現得像序列,*variable_names是把你的函數調用到這一點:

sortbyCounty(aggregate_file, sorted_file, 'C', 'T', 'Y', 'C', 'O', 'D', 'E', '9', '0') 

當你顯然希望函數調用是這樣的:

sortbyCounty(aggregate_file, sorted_file, 'CTYCODE90') 

製作variable_names列表或元組的字符串應該這樣做。

相關問題