pass words from a line of txt to list

0

image that there is a txt like the following:

juan pepe 123 25 345
marcelo velasquez 345 45 566

where juan pepe, 123,25,345 are elements of a list called user1, which in turn belongs to a list called user.

Any ideas on how I could do it?

    
asked by R3gulus 09.11.2016 в 03:55
source

1 answer

0

To read the file and assign them to a list , it would be by using the Readlines The return value is just a List (an option)

nombrearchivo = "file.txt"
contenido=[]
with open(nombrearchivo) as dr:
    contenido = fdr.readlines()
#Comprobar Contenido Extraído
print contenido

Update Individual items in the list. using the split method (taking as an example that their values are separated by a space)

nombrearchivo = "file.txt"
contenido=[]

with open(nombrearchivo) as fin:
    contenido = [ linea.strip().split(' ') for linea in fin ]
print contenido
    
answered by 09.11.2016 в 04:13