I'm trying to extract the numbers presented in a text file to a variable in my Python code.
I want to extract the numbers labelled Optic Disc Location X and Y, and save them to the following variables "od_x" and "od_y".
Here's the structure of the text file:
Code version: $Id: GeneralProjection.m 5455 2017-09-19 12:54:06Z mverhoek $
Device model: California
Fovea Location Y (vertical): 1540
Fovea Location X (horizontal): 1980
Fovea Confidence: 0.686278
Optic Disc Location Y (vertical): 1520
Optic Disc Location X (horizontal): 1596
Optic Disc Confidence: 0.731152
Eye Laterality Prediction (L,R): L
Eye Steer Prediction (C,I,S,L,R): C
Projection Table Used: /media/dropbox/GeneralProjectionTables/California/otrans(0.75,0.88).mat
Maximum percent error in area measurement: 0.843522
Distance from fovea to transformation point in degrees: 0.788453
Here's the code I currently have:
import re
for line in open('test.txt'):
match = re.search('Optic Disc Location X (horizontal): (\d+)', line)
if match:
od_x = match.group(1)
print(od_x)
for line in open('test.txt'):
match = re.search('Optic Disc Location Y (vertical): (\d+)', line)
if match:
od_y = match.group(1)
print(od_y)
What am I doing wrong? Is there an issue with my regex, or the way I'm assigning the result to the variable? Thank you :)