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)