我在使用pyhon3.4运行以下代码时报错:AttributeError: '_csv.reader' object has no attribute 'next'
import csvimport numpy as npwith open('C:/Users/Administrator/Desktop/data/titanic.csv', 'rb') as csvfile: titanic_reader = csv.reader(csvfile, delimiter=',', quotechar = '"') # Header contains feature names row = titanic_reader.next() feature_names = np.array(row) # Load dataset, and target classes titanic_X, titanic_y = [], [] for row in titanic_reader: titanic_X.append(row) titanic_y.append(row[2]) #The target value is "survived" titanic_X = np.array(titanic_X) titanic_y = np.array(titanic_y)
解决方案:
For version 3.2 and above
Change: csv_file_object.next()
To: next(csv_file_object)
then I get another error:
_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)
Edit: Figured it out needed to change rb to rt
Finally, it works.
REF.
[1]