Rohit Alekar
Rohit Alekar

Reputation: 89

Find the String and the Line number in the Text - python

I would like to search for a string in a large text and retrieve its line number. Is there a method that doesn't include 2 for loops in python.

Upvotes: 0

Views: 14711

Answers (4)

Dr. S
Dr. S

Reputation: 121

You could use filter to filter it out. Provide a lambda-function which would be true for the condition you want (eg. here it is match of the line).

And as the second parameter, give a list (iterator) of all the lines you want to check. Please note that I use izip, to have an iterator of the (line, line-number) tuple to the lambda function.

Please find the function below: As you can see, the limitation here is that this will work only for a file with less than 2^31 - 1 lines.

Also, note that, it returns a list of the line-numbers, of all the matching lines.

from itertools import izip
def find_line_num_in_file(file, line):
    f = open(file, "r")
    matches = filter(lambda x: line in x[0], izip(f.readlines(), xrange(-1 + 2**31)))
    f.close()
    return [m[1] for m in matches]

If you happen to have the lines already in possession (i.e., not an iterator), you could do this.

def find_line_num_in_lines(lines, line):
    matches = filter(lambda x: line in x[0], zip(lines, range(len(lines))))
    return [m[1] for m in matches]

Upvotes: 1

avasal
avasal

Reputation: 14854

this should give you the index

In [112]: lines = filehandle.readlines()

In [113]: for elem in lines:
   .....:     if elem.find(substr) > -1:
   .....:         print lines.index(elem)
   .....:

including all the index's of multiple occurances of substr

In [122]: text = ['abc', 'def', 'ghi']

In [123]: for elem in text:
   .....:     if elem.find('e') > -1:
   .....:         print text.index(elem)
   .....:
1

Upvotes: 0

eumiro
eumiro

Reputation: 212885

for i, line in enumerate(filehandle, 1):
    if text in line:
        print i, line

Upvotes: 6

Vinayak Kolagi
Vinayak Kolagi

Reputation: 1881

try:
    lstLines = fileHandle.readlines():
    lineNumber = lstLines.index("strSearch")
except:
    print "not found"

Upvotes: -3

Related Questions