I want to give two splits one to delete lines of txt and another to delete the tab of it in txt has name: surname ex Rafael: Lima the separator and ":" how can I do this?
I want to give two splits one to delete lines of txt and another to delete the tab of it in txt has name: surname ex Rafael: Lima the separator and ":" how can I do this?
I do not know how you're opening the file, but here's what I think is the best way:
with open('FICHEIRO.txt') as f:
for line in f:
nome, apelido = line.split(':') # nome = Rafael, apelido.split() = Lima
If you want to save all names eg:
Rafael: Lima from Carla: Borges
You can:
nomes = []
with open('FICHEIRO.txt') as f:
for line in f:
nome, apelido = line.split(':') # separar linha pelos dois pontos
nomes.append((nome, apelido.strip())) # remover a quebra de linha no apelido
print(nomes) # [('Rafael', 'Lima'), ('Carla', 'Borges')]