我有以下代码来遍历csv文件。使用csv文件第11列
中的最后一列进行迭代我收到以下错误:
from operator import itemgetter
import os
import csv
reader = csv.reader(open('file1.csv','rb'))
#header = reader.next()
header = next().reader
data = list(reader)
reader = None
data.sort(key=itemgetter(11))
writer = csv.writer(open('2.csv', 'wb'))
writer.writerow(header)
writer.writerows(data)
代码是:
function FilterPatientListBasedOnLiveSearch(filterVal) {
// Create a new array to store the Patients at multiple points
filterVal = filterVal.toLowerCase();
var trustArray = PatientList.map(function (patient) {
// return patient if search is empty or last name matches
if (!filterVal || patient.patientSurname.toLowerCase().indexOf(filterVal) !== -1) {
return "<tr id=" + patient.PatientID + "><td class='patientListNames'>" + patient.patientFirstname + " " + patient.patientSurname + "</td></tr>"
}
});
$('#dataTables-example').find('tbody').html(trustArray);
}
我认为这是抱怨标题。
答案 0 :(得分:3)
您需要将reader
作为参数传递给next()
function:
header = next(reader)
或者您可以在阅读器对象上调用iterator.next()
method:
header = reader.next()
我建议坚持第一次拼写,特别是因为如果CSV文件为空,你可以告诉它返回None
(或其他默认值):
header = next(reader, None)