Get CSV list with Python

Asked

Viewed 355 times

1

I have a CSV file with more than 9000 lines. I need to get a list with only one data of each line.

An example is the following:

01/02/19,carlos,casa,rio
03/05/18,luis,carro,maceio
06/04/17,camila,celular,teresina

I need to get a list of names only:

lista = ['carlos','luis','camila']

the closest I’ve come to that code:

csvRows=[]
arq_contas = open ('C:.../lista_contas.csv')
leitura_contas = csv.reader(arq_contas,delimiter=',', lineterminator='\n')
for row in leitura_contas:
    csvRows.append(row[1:2])
    print(csvRows)

but I got the following result:

['carlos'], [], ['luis'], [], ['camila'], [],

I am a beginner in Python and programming with a whole. That’s why I need a light from you.

  • Tries to replace row[1:2] for row[1]

  • the result was only: ['carlos']

  • Only the first is ['carlos'], but at the end of the loop will be ['carlos', 'luis', 'camila']. See here.

  • when I run mine appears only ['carlos'] and then this error: csvRows.append(Row[1]) Indexerror: list index out of range

  • So it’s likely that you’re running a different code than the question.

  • found the error. On each line there is an n. (https://repl.it/repls/DarksalmonAliceblueNanocad)

Show 1 more comment

2 answers

1

Like you showed, the problem is that your CSV file is not formatted properly as there is a blank line between the records. Not that it invalidates, in fact, the format, but basically every blank line would be a null record, which would not make much sense.

From your code, the only correction needed to generate the desired result is to change row[1:2] for row[1], because the first way you’d be picking up two columns and not just one.

A slightly more elaborate solution would be:

import csv

def get_csv_column(filename, column):
    with open(filename) as stream:
        reader = csv.reader(stream)
        for row in reader:
            yield row[column]

print( list(get_csv_column('dados.csv', 1)) )

See working on Repl.it

1

Good morning Anderson, thank you very much for your attention, but with your code the error continues:

IndexError: list index out of range

really is an error caused by CSV formatting, but I am not able to fix it.

Today I tested the code:

entrada = open('C:/...arquivo.csv','r')
saida = open('C:/...saida_arquivo.csv','w')
for linha in entrada:
    l = str(linha.split(','))
    saida.write(l)
entrada.close()
saida.close()

with this code I obtained the following:

['02/22/18', 'rosalvaponteatelierdecriacao', 'General', 'liked\n']['\n']['02/22/18', 'idealaeventobar', 'General', 'liked\n']['\n']['02/22/18', 'jasmim_wedding_trend', 'General', 'liked\n']['\n']

Browser other questions tagged

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