-1

I have a text file from which i want to extract values and create a rectangle from each x and y value the values in the file look like this

.io_tile 0 1 .io_tile 0 2 .io_tile 0 3 .io_tile 0 4 .io_tile 0 5    .io_tile 0 6 .io_tile 0 7 .io_tile 0 8 .io_tile 0 9 .io_tile 0 10    .io_tile 0 11 .io_tile 0 12 .io_tile 0 13 .io_tile 0 14 .io_tile 0 15    .io_tile 0 16 .io_tile 1 0 .io_tile 1 17 .io_tile 2 0 .io_tile 2 17    .io_tile 3 0

I have to ignore the .io_tile part for each value and only get the integer values. I know how to create a rect in pygame, but I have no clue how to get values from the file.

Prune
  • 76,765
  • 14
  • 60
  • 81
Yasir
  • 25
  • 7
  • If you Google the phrase "Python file input", you’ll find tutorials that can explain it much better than we can in an answer here. – Prune Sep 27 '18 at 16:47

1 Answers1

1

This is gonna focus on how to manage the data once you've read it. For guidance how to read a file I'll recommend other answers on this site.

If your raw text is stored in a variable named text you can split the data and store it into a list with the following code

coordinates = map(lambda x: tuple(map(int, x.split())), text.split(".io_tile "))

coordinates will then contain a list of tuples

[(0, 1), (0, 2,), (0, 3)... ]
Johan
  • 3,577
  • 1
  • 14
  • 28