Get a certain column from a multi-column file

0

Personal I have a text file with some information separated by ';' for each line example:

Nome;Email;Telefone;Endereço;Civil

I need to 'filter' only the 3 field (Address since it starts from 0) of each line, how can I do this?

This is my code so far but it only takes 4 letter lines:

def getParameters(dirBkp):
    x = 0
    with open(dirBkp, "r") as arq:
        for linha in arq:
            print(linha[3])
    
asked by anonymous 16.08.2018 / 21:14

1 answer

3

Use the csv module. With it you can define the column separator and even read each line as a dictionary, making it easier to read the code.

import csv

def get_address_from_file(filename):
    with open(filename, 'r') as stream:
        reader = csv.DictReader(stream, delimiter=';')
        for row in reader:
            yield row['Endereço']

So, if you have a file like, for example:

Nome;Email;Telefone;Endereço;ECivil
Foo;[email protected];0;Brasilia;Solteiro
Bar;[email protected];0;Curitiba;Casado

Just do:

for endereco in get_address_from_file('arquivo.txt'):
    print(endereco)

The output would be:

Brasilia
Curitiba

See working at Repl.it

You can still generalize the function by passing the column name by parameter, if you need to access other columns at other times:

def get_column_from_file(filename, column):
    with open(filename, 'r') as stream:
        reader = csv.DictReader(stream, delimiter=';')
        for row in reader:
            yield row[column]

And do:

for endereco in get_column_from_file('arquivo.txt', 'Endereço'):
    print(endereco)
    
16.08.2018 / 21:30