AttributeError: '_csv.reader' object has no attribute 'next'

我在使用pyhon3.4運行如下代碼時報錯:AttributeError: '_csv.reader' object has no attribute 'next'html

import csv
import numpy as np
with 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)

解決方案:python

For version 3.2 and aboveapp

Change: csv_file_object.next()htm

To: next(csv_file_object)ip

then I get another error:get

_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)string

Edit: Figured it out needed to change rb to rtit

Finally, it works.io

 

REF.class

[1]https://www.kaggle.com/c/titanic/forums/t/4937/titanic-problem-with-getting-started-with-python

相關文章
相關標籤/搜索