Next() in CSV Reader with Python 3

Asked

Viewed 1,153 times

3

Hello, I am taking a course of Machine Learning/Classification and well it uses a CSV file in which one should ignore the first line of the file. So 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" has no NEXT attribute. Someone could help me?

Return from the Terminal:

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 class the code is correct and should work. But I tried to look into the change in Next, and I tested other codes I found.

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

3 answers

3


You need to use the function next() instead of a method with that name.

Thus:

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

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

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

  • I’m sorry, but I don’t think you quite understand what I mean. If my code is not implementing next() it means I need to implement next ? But where would this be implemented? Sorry, I’m discovering the world of programming and there are many things that still leave me a little confused.

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
  • Well, I tried this way and he continued not accepting. Saying that there was something wrong with Iterator.

  • http://carlosmaniero.blogspot.com.br/ face of a look at this blog, might help

0

I use version 3.61 here, I put the code in this way and it worked 100% what should be noted is attention to details, because you put the reading of a csv(txt) file as "Rb" read Binary ? I just put "r" on and it’s all right! import csv

def load_accesses(): 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

upload()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.