how to give two splits in one txt [closed]

1

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?

    
asked by anonymous 26.04.2017 / 20:12

1 answer

1

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')]
    
26.04.2017 / 20:15