Get specific column csv with python

0

I have a method that opens a CSV file but I would like to go directly to a specific column, I tried to do list2 = [row.split()[3] for row in f] , but it returns a piece of the file, not the column. Is it possible to do this without using PANDAS? My method:

import csv
list2 = []
count = 0

with open('meuarquivo.csv') as f:
    list2 = [row.split()[3] for row in f]

while(count <= 20):
    print(list2[count])
    count+=1
    
asked by anonymous 08.10.2018 / 23:02

2 answers

2

Yes, you can read a specific column without using external libraries like pandas , your code does not work because you do not specified column separator in split . I created a variable for the tab if you are using another, just change.

''' Conteudo CSV
nome,idade
Laerte,23
Maria,18
'''

separador = ','

with open('teste.csv', 'r') as txt_file:
    for line_number, content in enumerate(txt_file):
        if line_number:  # pula cabeçalho
            colunas = content.strip().split(separador)
            print(f"Nome: {colunas[0]}, \nIdade: {colunas[1]}")

The if is to check if it is not the header line but the line containing the data.

    
08.10.2018 / 23:13
1

Just use the csv module you imported in your example using the DictReader , for example:

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

So for a CSV:

seq,firstname,lastname,age
1,Helen,King,58
2,Emilie,Joseph,59
3,Bess,Frazier,29
4,Amy,Gross,33
5,Olga,Sutton,62
6,Gary,Moreno,54
7,Myrtie,Freeman,47
8,Philip,Adkins,32
9,Thomas,Morales,64
10,Stella,Rodgers,43

We could do

for name in get_column_of_csv('data.csv', 'firstname'):
  print(name)

Getting the result

Helen
Emilie
Bess
Amy
Olga
Gary
Myrtie
Philip
Thomas
Stella

Note that by default, the DictReader class will consider the first line of the CSV file to be the name of the columns, so it was possible to identify the firstname column in the code.

    
09.10.2018 / 01:39