Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Conditional RegEx
#1
Hi,
I'd like the RegExs below to output 'NULL' when they don't find a match but I haven't figured out how to do this. I'd like to do this so that when I can display flight1 and flight2 in a DataFrame. Otherwise, I get the error 'All arrays must be the same lenght'. Can anyone help?

for i in range(0,5):
    text = doc.getPage(i).extractText()
flight1_re = re.compile(r'E *\d{3}\s') 
    for match in flight1_re.findall(text):
        flight1 = match(text)
    else:
        flight = 'NULL'
        print(flight)

flight2_re = re.compile(r'E *\d{3}\s') 
    for match in flight2_re.findall(text):
        flight2 = match(text)
    else:
        flight2 = 'NULL'
        print(flight2)
Output:

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-d985b6080e6e> in <module>
21 flight_re = re.compile(r'E *\d{6}\s')
22 for match in flight_re.findall(text):
---> 23 flight = match(text)
24 else:
25 flight = 'NULL'

TypeError: 'str' object is not callable
Reply


Messages In This Thread
Conditional RegEx - by pprod - Feb-18-2021, 02:10 PM
RE: Conditional RegEx - by bowlofred - Feb-18-2021, 05:49 PM
RE: Conditional RegEx - by pprod - Feb-19-2021, 09:19 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020