-1

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 :)

2567655222
  • 167
  • 7

1 Answers1

0

You need to escape the parentheses in your regex for them to be considered litteral parentheses, and not a group. Also, it's better to use raw strings to avoid escaping problems. Just change to:

import re
for line in open('test.txt'):
    match = re.search(r'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(r'Optic Disc Location Y \(vertical\): (\d+)', line)
    if match:
        od_y = match.group(1)
        print(od_y)

Output:

1596
1520

You can improve your code a bit by reading the file only once and testing both regexes in the same loop:

import re
  
re_x = re.compile(r'Optic Disc Location X \(horizontal\): (\d+)')
re_y = re.compile(r'Optic Disc Location Y \(vertical\): (\d+)')

with open('test.txt') as f:
    for line in f:
        match = re_x.search(line)
        if match:
            od_x = match.group(1)

        match = re_y.search(line)
        if match:
            od_y = match.group(1)

print(od_x)
print(od_y)

And even, since Python 3.8, you can use the new := operator to test and assign the match at the same time:

import re
  
re_x = re.compile(r'Optic Disc Location X \(horizontal\): (\d+)')
re_y = re.compile(r'Optic Disc Location Y \(vertical\): (\d+)')

with open('test.txt') as f:
    for line in f:
        if match := re_x.search(line):
            od_x = match.group(1)

        if match := re_y.search(line):
            od_y = match.group(1)

print(od_x)
print(od_y)
Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50