Next () in CSV Reader with Python 3

3
Hello, I'm doing a Machine Learning / Classification course and well it uses a CSV file in which one should ignore the first line of the file. Then I made the following code:

import csv

def carregar_acessos():

    X = []
    Y = []

    arquivo = open('acesso_pagina.csv', 'rb')
    leitor = csv.reader(arquivo)
    leitor.next()

    for home,como_funciona,contato,comprou in leitor:

        dado = [int(home),int(como_funciona),int(contato)]
        X.append(dado)
        Y.append([int(comprou)])

But when the executor informs that the "reader" does not have NEXT attribute. Could someone help me?

Terminal Return:

Traceback (most recent call last):
  File "classifica_acesso.py", line 3, in <module>
    X,Y = carregar_acessos()
  File "/Users/josecarlosferreira/Desktop/machine-learning/dados.py", line 11, in carregar_acessos
    leitor.next()
AttributeError: '_csv.reader' object has no attribute 'next'

According to what is shown in the lesson the code is correct and should work. But I tried to look for the change in Next, and I tested other codes that I found and well kept giving error.

Note: The first line of the CSV file is a Text and all others are integers.

    
asked by anonymous 09.11.2017 / 01:00

3 answers

2

You need to use the next() function rather than a method with that name.

So:

    leitor = csv.reader(arquivo)
    next(leitor)

In the standard Python library, iterators implemented in C directly support the next() function. Classes implemented in Python can implement a method called __next__ to support the next() function.

Regardless of how the iterator is implemented, the recommended way to access the next item is to call next(meu_iterador) .

    
09.11.2017 / 03:26
0
import csv

def carregar_acessos():
    X = []
    Y = []

    arquivo = open('acesso.csv', 'rb')
    leitor = csv.reader(arquivo)

    next(leitor)

    for home,como_funciona,contato, comprou in leitor:

        dado = [int(home),int(como_funciona)
            ,int(contato)]
        X.append(dado)
        Y.append(int(comprou))

    return X, Y
    
09.11.2017 / 02:32
0

I use version 3.61 here, I put the code this way and it worked 100%, what should be observed is attention to detail, since you put the reading of a csv (txt) file as "rb" read binary? I just put "r" and ready all right! import csv

def load_access ():     X = []     Y = []

arquivo = open('csvpy.txt', 'r')
leitor = csv.reader(arquivo)

next(leitor)

for home,como_funciona,contato, comprou in leitor:

    dado = [int(home),int(como_funciona)
        ,int(contato)]
    X.append(dado)
    Y.append(int(comprou))
    print(X)
    print(Y)

return X, Y

load_access ()

    
14.11.2017 / 19:27