How to read integers in a file in Python?

  python, readfile

I want to read integers in a file using Python.
There are some solutions but it does not work in my case. My file looks like this

50:32 9 5 436 475 3453
40:53 63 634 26 62
44:545 63 6246 6344 6346 44
90:52 6346 65 634 63

I tried this code

for line in fr.readlines():
  Semi = line.split(':')
  Targets.append(int(Semi[0]))
  Space = line.split(' ')
  cubes.append(int(Space[1]))

But it does not work the output

[50, 40, 44, 90]

I need to read integers before (:) and store them in an array (Actually I did it successfully). But I need to store every line in an array for example the first line

[32, 9, 5, 436, 475, 3453]

Source: Python Questions

LEAVE A COMMENT