Python RegEx to report lines a string exists on
Question: I am trying to write something up real quick that will find a specific string
and report to me what lines those string exist on. I am trying to find
exponents so I am looking for "e+" which only occurs on lines that start with
AAA, but not ALL the lines with AAA at the start have e+ in them. Currently,
my code looks like this:

    
    
    import re
    file = raw_input("Enter a file to scan ")
    extfile = file+".txt"
    #NDoc = raw_input("Enter a file to place results ")
    #log = open (NDoc, 'w')
    xfile = open(file+".txt")
    
    expcnt = 0
    nl = list
    
    
    for line in xfile:
            line = line.strip()
            n = re.findall('^PV1.+(e\+)')
            if len(n) > 0:
                expcnt = expcnt+1
    ###            l = line n is on
                nl.append(l)
                for item in nl:
    #                log.write(item+"\n")
    
    
    
    
    
    print (expcnt,"exponenets exist and occur on the following lines:"
    For item in nl:
            print item
    #print ("Your results can be found in",extfile,".") 
    

Basically, I want to know what I need to put on the line with 3 hashtags(#) in
order to record the line n was found on and put it into a list. Later, I want
to print that list out. NDoc and log at the top are commented out because I am
considering print the results to a new page using `from __future__ import
print_function`, but that can be ignored for now. So I am trying to put all
line numbers into the list nl and all of the exponent strings into the list el
(though list el is just for me to double check things and wont be used later).

Any assistance would be much appreciated, thanks in advance!

Edit:

    
    
    import re
    file = raw_input("Enter a file to scan ")
    extfile = file+".txt"
    #NDoc = raw_input("Enter a file to place results ")
    #log = open (NDoc, 'w')
    xfile = open(file+".txt")
    
    expcnt = 0
    nl = list
    
    
    for line_num, line in enumerate(xfile):
            line = line.strip()
            n = re.findall('^PV1.+(e\+)')
            if len(n) > 0:
                expcnt = expcnt+1
                l = line_num
                nl.append(l)
                for item in nl:
    #                log.write(item+"\n")
    
    
    
    
    
    #print (expcnt,"exponenets exist and occur on the following lines:"
    for item in nl:, does anyone know why that would occur?
        print item
    #print ("Your results can be found in",extfile,".")  
    

For some reason this is returning an error "Expected an indented block" at
`for itme in nl:`


Answer: Since you're already reading the file line by line, you could use `enumerate`.

    
    
    for line_num, line in enumerate(xfile):
    

instead of

    
    
    for line in xfile:
    

