read numbers together in a txt?

-1

I have the following list of numbers which are in a .txt:
6 28 12 3 10 22 26 7 10
I need the program to know that what it reads is a 28, 12 or 22 for example. to then put them on a list.

    
asked by AXL 04.11.2018 в 02:00
source

1 answer

3

With this we read the file and save it in a variable. The code with makes sure to open and close the file when the block is executed.

with open("mifichero.txt", "r") as leyendo:
  datos = leyendo.read()

Now simply put them in a list. The split command will divide the numbers each time a space is found.

mi_lista = datos.split(" ")

But all that is text, if you need it as integrity, save it like this:

mi_lista = [int(x) for x in datos.split(" ") if not " " in x ]

To check:

print(mi_lista)
    
answered by 04.11.2018 в 03:56