Iterator should return a String

Asked

Viewed 68 times

2

Hello, I have the following code:

import csv

def carregar_acessos():

    X = []
    Y = []

    arquivo = open('acesso_pagina.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)])

He tries to pull information from a CSV file containing several ZEROS and UNS. And I have another file that shows this in the terminal:

from dados import carregar_acessos

X,Y = carregar_acessos()

print(X)
print(Y)

But when I run the code the following error appears:

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 10, in carregar_acessos
    next(leitor)
_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)

Well, I saw some answers in Stackoverflow in English, but even trying the solutions there proposed the code did not work and kept keeping the same error.

The CSV is created in this way:

home,como_funciona,contato,comprou
1,1,0,0
1,1,0,0
1,1,0,0
1,1,0,0
1,1,0,0
1,0,1,1
1,1,0,0
1,0,1,1
1,1,0,0
1,0,1,1

And it goes on like this for another 200 lines. They each indicate a YES(1) and NO(0) information. Could someone help me?

  • How is the structure of your csv?

  • I edited the topic to demonstrate!

2 answers

2

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
  • Sometimes things are in our face and we are hunting something more complex and we forget the Basics! Thanks Marcelo.

  • good thing it was a re-turn

  • I thought I had solved it. I went to try to run and it didn’t work! Keeps giving the same error!

0

With the function read_csv of pandas it is simpler to read your table, in the file dados.py:

import pandas as pd

def carregar_acessos():
    
    arquivo = pd.read_csv('acesso_pagina.csv')
    return arquivo

and in the main archive:

from dados import carregar_acessos

df = carregar_acessos()
print(df)

finally, the exit will be:

   home  como_funciona  contato  comprou
0     1              1        0        0
1     1              1        0        0
2     1              1        0        0
3     1              1        0        0
4     1              1        0        0
5     1              0        1        1
6     1              1        0        0
7     1              0        1        1
8     1              1        0        0
9     1              0        1        1

Browser other questions tagged

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