pythonreadlineread-data

how do I read in numbers from a file and assign variables to them when the numbers have some text before them on the line in the file


I am reading in data from a text file in python. one of the lines that is being read is the following

Grid-ref= 4, 323

I want to be able to assign the 4 and 323 to variables xref and yref respectively. This line appears several times throughout the file with different numbers so i want to change xref and yref each time this line appears. The text 'Grid-ref= ' appears each time. How do I ignore the first part and be able to access the values to store them in variables? Either the first part can be ignored, or i can do an if statement for 'if the line starts with Grid-ref'. Either would be fine but i dont know how to do either of them.

I know which line numbers the Gridref would appear in, it is lines 11x - 6, where x is an integer starting from 1, and incrementing by 1 to a large number


Solution

  • Assume you got each line separated:

    line = ‘Grid-ref= 4, 323’
    x,y = line[9:].replace(" ", "").split(',')