[Python] Regex capture issues parsing work schedule - other viable options?

Discussão em 'Python' iniciado por Stack, Maio 20, 2025.

  1. Stack

    Stack Membro Participativo

    I've recently starting learning Python, and I'm creating a small project to track my work commute. I work in aviation, so the basis for this project is parsing my work schedule (roster) to an SQLite3 db.

    I'm using pypdf to parse the roster from a .pdf file, and I've written a regex to try to capture the flights for each day. The regex is not capturing everything I thought it would, so I'm hoping for some tips / feedback on what I'm doing wrong. I'm also open to suggestions on how to better accomplish this task.

    A snippet of the string parsed from the roster pdf would look like this:

    ...[xFDP 00:00][DTwSB 08:00]\nThu22 C/I ABC 0530\nXX 1260 ABC 0630 !1135 DEF S7M8XX 1261 DEF 1220 !1540 ABC S7M8\nC/O!1600 ABC [FT 08:25]\n[DT 10:30][FDT 10:10]...


    The regex I've come up with is this:

    flight_duty_regex = re.compile(r'''(
    (?:Mon|Tue|Wed|Thu|Fri|Sat|Sun) # Weekday
    (\d{2})\s # Date
    C/I\s # Check-in
    (\w{3})\s # Check-in station
    (!?\d{4})\s # Check-in time (optional ! for different timezone)
    (?:(XX|YY)\s # Prefix
    (\d{3,4})\s # Flight number
    (\w{3})\s # Departure station
    (!?\d{4})\s # Departure time (optional ! for different timezone)
    (!?\d{4})\s # Arrival time (optional ! for different timezone)
    (\w{3})\s # Arrival station
    (?:S7M8|S738)\s?)+ # Aircraft type
    C/O # Check-out
    (!?\d{4})\s # Check-out time (optional ! for different timezone)
    (\w{3}) # Check-out station


    )''', re.VERBOSE)


    Which produces the following output:

    [('Thu22 C/I ABC 0530\nXX 1260 ABC 0630 !1135 DEF S7M8XX 1261 DEF 1220 !1540 ABC S7M8\nC/O!1600 ABC', '22', 'ABC', '0530', 'XX', '1261', 'DEF', '1220', '!1540', 'ABC', '!1600', 'ABC')]


    I've added the whole regex as a group just to show that both flights (XX 1260 and XX 1261) is captured initially. However, when listing the individual groups only the last flight, XX 1261, is listed. I've also tested this with text strings containing more that two flights, and still it only lists the last flight.

    What am I missing?

    Also: is there a better way to do this? The text string from the roster pdf is very structured, but there are some variations in how many flights I have per day etc. Is there an easier way to capture this data? Should i use named groups, and search each line of text instead of using .findall()?

    Continue reading...

Compartilhe esta Página