anonymous13
anonymous13

Reputation: 621

Unable to search and compile regex code from each line in python

I am trying to write a program to match regex in the file. Initial lines of my file looks as shown below

Alternate Take with Liz Copeland (Day 1) (12am-1am)                    
    Saturday  March 31, 2007                    
        No.    Artist    Song    Album (Label)    Comment
    buy    1    Tones on Tail    Go! (club mix)    Everything! (Beggars Banquet)    
    buy    2    Devo    (I Can't Get No) Satisfaction    Anthology: Pioneers Who Got Scalped (Warner Archives/Rhino) 

My code to match first line of the file is as follows

with open("data.csv") as my_file:
  for line in my_file:
      re_show = re.compile(r'(Alternate Take with Liz Copeland) \((.*?)\)\s\((.*?)\)')
      num_showtitle_lines_matched = 0
      m_show = re.match(re_show, line)
      bool(m_show) == 1
      if m_show:
         num_showtitle_lines_matched += 1

         show_title =  m_show.group()

print("Num show lines matched --> {}".format(num_showtitle_lines_matched))
print(show_title)

It should give me result as below

Alternate Take with Liz Copeland (Day 1) (12am-1am)
num_showtitle_lines_matched -->1

But my result doesn't show any output. Please let me know how to accomplish this.Thanks in advance.

Upvotes: 1

Views: 49

Answers (1)

gaw
gaw

Reputation: 1960

As in the comment: just put the num_showtitle_lines_matched = 0 above the loop:

with open("data.csv") as my_file:
  num_showtitle_lines_matched = 0
  for line in my_file:
      re_show = re.compile(r'(Alternate Take with Liz Copeland) \((.*?)\)\s\((.*?)\)')      
      m_show = re.match(re_show, line)     
      bool(m_show) == 1
      if m_show:
         num_showtitle_lines_matched += 1
         show_title =  m_show.group()
print("Num show lines matched --> {}".format(num_showtitle_lines_matched))
print(show_title)

Output:

Num show lines matched --> 1
Alternate Take with Liz Copeland (Day 1) (12am-1am)

Upvotes: 1

Related Questions