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
然而,當我print
我headers
名單,我可以清楚地看到我的變量是存在的:
['_STATE90', 'HEIGHT90', 'WEIGHT90', '_BMI90', 'AGE90', 'CTYCODE90', 'IYEAR90', 'SEX90', '_RFOBESE90']
所以我不明白爲什麼我收到這個錯誤信息。我錯過了什麼?