1. 程式人生 > >AttributeError: '_csv.reader' object has no attribute 'next' 我在使用pyhon3.4執行以下程式碼時報錯:AttributeError:

AttributeError: '_csv.reader' object has no attribute 'next' 我在使用pyhon3.4執行以下程式碼時報錯:AttributeError:

我在使用pyhon3.4執行以下程式碼時報錯:AttributeError: '_csv.reader' object has no attribute 'next'

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

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)

解決方案:

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.