AttributeError: '_csv.reader' object has no attribute 'next'
我在使用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.
[1]https://www.kaggle.com/c/titanic/forums/t/4937/titanic-problem-with-getting-started-with-python